2
votes

I created a Custom Field with the Repeater layout to add some input text. I would like to display all the values. I found some code on ACF documentation but I can't understand how it works

<?php 
$rows = get_field('repeater_field_name');
if($rows)
{
    echo '<ul>';

    foreach($rows as $row)
    {
        echo '<li>sub_field_1 = ' . $row['sub_field_1'] . ', sub_field_2 = ' . $row['sub_field_2'] .', etc</li>';
    }

    echo '</ul>';
}
?>

http://www.advancedcustomfields.com/resources/repeater/

I don't know how much fields I will create with the Repeater and I would like to loop all the values with foreach. Is that possible?

Thank you in advance

enter image description here enter image description here

2
the code that you found will loop all the values from the repeater field, just change field_name with your field names (slug names) and it will work.Christophvh
It doesn't works! :( I put my repeater_field_name: get_field('MY_repeater_field_name'); and my field_name: $row['MY_sub_field_1'] but it doesn't works. It returns me this: sub_field_1 = , sub_field_2 = , etc sub_field_1 = , sub_field_2 = , etcMaki
maybe a stupid question, but have you added something in your post? because it seems like it works but there is no value in the fields?Christophvh
Yes, sure. I put some values in the custom fields. With the repeater I create two custom fields.. Is there another way to display these values?Maki
I found also this code: link Under Working with Array values. I put my Repeater_field_name and it returns me this: array(2) { [0]=> array(1) { ["testo"]=> string(10) "My Value 1" } [1]=> array(1) { ["testo"]=> string(10) "My Value 2" } } So can this code help me?Maki

2 Answers

2
votes

Foreach version:

<?php 

$rows = get_field('repeater');
if($rows)
{
    echo '<ul>';

    foreach($rows as $row)
    {
        echo '<li>sub_field_1 = ' . $row['text'] . '</li>';
    }

    echo '</ul>';
}

While version:

<?php

// check if the repeater field has rows of data
if( have_rows('repeater') ):

    // loop through the rows of data
    while ( have_rows('repeater') ) : the_row();

        // display a sub field value
        the_sub_field('text');

    endwhile;

else :

    echo 'nothing found';

endif;

?>
0
votes

I would fix it like this:

<?php
if( have_rows('slide') ): 
  $l= 1;
  while( have_rows('slide') ): the_row();       
    $l++;
  endwhile; 
endif;  
?>