Skip to content

Advanced Custom Fields (ACF)

Text Fields

Basic usage:

<?php the_field('heading'); ?>

// Subfield for repeaters
<?php the_sub_field('heading'); ?>

Blog archive

When adding custom fields for the posts archive page, set the location as Page Type is equal to Posts Page.

Then on the index.php or archive.php to access the value:

<?php the_field('page_heading', get_queried_object_id() ); ?>

Options page

Add an options page to the admin menu.

Add options page

https://www.advancedcustomfields.com/resources/options-page/

Requires Pro version.

Add to functions.php to install.

if( function_exists('acf_add_options_page') ) {

  // Basic minimal
  acf_add_options_page();

  // Footer example
  acf_add_options_page(array(
    'page_title'  => 'Footer Settings',
    'menu_title'  => 'Footer',
    'menu_slug'   => 'footer-settings',
    'capability'  => 'edit_posts',
    'redirect'    => false
  ));

}

Then, for example, to use a field called heading:

<?php echo get_field('heading', 'option'); ?>

Groups

Groups can help organise inputs in the admin screen. Once you have fields inside a group you can access them like so:

$tabulated_group = get_field('tabulated');
$img1 = $tabulated_group['image_1'];

Then we can use $img1 as per normal.

<img
  src="<?php echo $img1['url']; ?>"
  alt="<?php echo $img1['alt']; ?>"
  class=""
  loading="lazy"
  width="<?php echo $img1['width']; ?>"
  height="<?php echo $img1['height']; ?>"
/>

Repeater

Base code:

<?php
  if( have_rows('repeater_field_name') ):
    while( have_rows('repeater_field_name') ) : the_row();
?>

  <!-- HTML -->
  <?php the_sub_field('sub_field'); ?>

<?php
    endwhile;
  endif;
?>