YogeshChauhan.com
WordPress: How to print ACF repeater field values?
February 22, 2021

I demonstrated different loop examples to print Advanced Custom Fields array field values in this post.

We can use loops to print ACF repeater contents as well.

The fields inside the repeater can be accessed by get_field or the_sub_field. We can also use the_repeater_field instead of the_sub_field.

Saving the repeater fields in a variable



if( have_rows('repeater_field_name') ):
  while( have_rows('repeater_field_name') ): the_row();
    $sub_field_1 = get_sub_field('sub_field_1');
    $sub_field_2 = get_sub_field('sub_field_2');
    //do something with the variables
  endwhile;
endif;


Printing the repeater fields



if( have_rows('repeater_field_name') ):
  while( have_rows('repeater_field_name') ): the_row();
    the_sub_field('sub_field_1');
    the_sub_field('sub_field_2');
  endwhile;
endif;


Repeater inside repeater



if( have_rows('repeater_field_name') ):
  while( have_rows('repeater_field_name') ): the_row();
    if( have_rows('repeater_field_2_name') ):
      while( have_rows('repeater_field_2_name') ): the_row();
        the_sub_field('sub_field_1');
        the_sub_field('sub_field_2');
      endwhile;
    endif;
  endwhile;
endif;


Randomly selecting a repeater field row



$rows = get_field('repeater_field_name');
$row_count = count($rows);
$i = rand(0, $row_count - 1);

echo $rows[ $i ]['sub_field_name'];


dreamhost

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 2 How to zoom an element on hover using CSS? Mar 2 the box-sizing property in CSS Mar 2 WordPress: How to access first and random row values from a repeater field in ACF? Mar 1 The basics of CSS Box model Feb 27 WordPress: How to display slider repeater fields in ACF? Feb 26 Simple Page Hit Counter in PHP

You might also like these

JavaScript String Properties and MethodsJavaScriptThe flex-grow, flex-shrink and flex-basis Properties in CSSCSSSome interesting HTML Input Attributes to rememberHTMLHow to modify the latest post array using get_posts() in WordPress?WordPressHow to Secure Web or Mobile Browsers?MiscCreate a responsive pricing table using simple HTML and CSSCSS