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 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
3 ways to pass a variable in url() function in SCSS (Sass)SCSSArbitrary Arguments in SCSS functionsSCSSSolution for Xcode 11 Command PhaseScriptExecution failed with a nonzero exit code errorMiscellaneousHow to compress images with gulp in WordPress?WordPressHow to concatenate variable with string in Swift?SwiftWhy do we need HAVING Clause in SQL?SQL/MySQL