Yogesh Chauhan's Blog

WordPress: How to print ACF repeater field values?

in WordPress on 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'];



Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to create a link tag button using CSS?CSSHow to add Read More Read Less Button using JavaScript?JavaScriptHow to convert an object from API to JSON array in Angular 9?AngularCreate a responsive image gallery using CSS gridCSSHow to swap images on hover using CSS?CSSKeyValuePipe in Angular 9Angular