3

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

Maki
  • 199
  • 2
  • 4
  • 13
  • 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 Jan 25 '16 at 10:44
  • 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 = , etc` – Maki Jan 25 '16 at 10:55
  • 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 Jan 25 '16 at 10:59
  • 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 Jan 25 '16 at 11:15
  • I found also this code: [link](http://www.advancedcustomfields.com/resources/code-examples/) 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 Jan 25 '16 at 11:20
  • that code shows that your array being filled, which is good, just post a screenshot of your Repeater field on the admin screen, and i will post an answer with the solution – Christophvh Jan 25 '16 at 11:22
  • Add the image screen above. thx – Maki Jan 25 '16 at 11:30
  • I'll tell you more: if I use the while cycle it works! Why with the foreach cycle doesn't work? – Maki Jan 25 '16 at 11:44
  • that's not the screenshot i meant.. I mean a screenshot of the 'Extra fields' settings so i can see the slug name etc. Also if the while is working, is your problem fixed or not? if so answer your own question with the solution. – Christophvh Jan 25 '16 at 11:56
  • Oh, sorry! :) I put the right image now. slug of repeater field: repeater. slug of text fields: text. Anyway I would like to understand why it doesn't work with the foreach loop.. – Maki Jan 25 '16 at 12:00

2 Answers2

5

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;

?>
Christophvh
  • 12,586
  • 7
  • 48
  • 70
0

I would fix it like this:

<?php
if( have_rows('slide') ): 
  $l= 1;
  while( have_rows('slide') ): the_row();       
    $l++;
  endwhile; 
endif;  
?>
UfguFugullu
  • 2,107
  • 13
  • 18