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'];
ACF advanced custom random repeater while