11 Aug 2017

Displaying advanced custom fields outside of the loop

The following solutions pertain to the excellent wordpress plug Advanced Custom Fields.

To display fields outside of teh wordpress posts loop use the following code but replace the post ID 123:


$value = get_field( "text_field", 123 );
echo $value;

More info here - and here's how to do it inside the loop.

What about repeater fields?

I'm glad you asked. Here's a PHP snippet to echo the values for a repeating field that I've called logo (just remember to change the ID 1862 to match the post or page ID):


while( have_rows('repeater', 1862) ): the_row();
the_sub_field('logo');
endwhile;

Output individual fields contained with a group field

Advanced Custom Fields allows the grouping of fileds into a single group field. The following will output individual sub fields (remember to replace the post ID):


$group_field = get_field( "my_group_field", 5563 );
echo $group_field['my_sub_field'];