ACF loop Repeater values with get_field

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/

Read More

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

Related posts

2 comments

  1. 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;
    
    ?>
    
  2. I would fix it like this:

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

Comments are closed.