YogeshChauhan . com

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'];


amazon

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

How to make a UILabel clickable in Swift 5?SwiftHow to create ‘share on LinkedIn’ link using just HTML?HTMLThe fundamental difference between HAVING and WHERE clauses in PostgresPostgresDifferent Types of Functions in JavaScriptJavaScriptWhat is Conditional Rendering in React?ReactHow to catch .keypress() on body using jQuery?jQuery