Search code examples
countrepeaterrowsadvanced-custom-fields

ACF Repeater display row number


Looking to display the individual number of each repeater row. So for example, the first row will display "1" and the second row will display "2".

I found this from Elliot,

<?php echo count( get_field('repeater_field') );?>

which counts how many rows there are in total. But I need the individual number next to each.

Thanks


Solution

  • I would suggest taking a look at Elliot's answer here:

    http://support.advancedcustomfields.com/forums/topic/getting-instance-and-sort-of-id-of-repeater-field/

    You would want to set up a counter variable ( $i ) and then add 1 to $i inside the loop.

    <?php if( have_rows('repeater_field') ): $i = 0; ?>
        <div class="repeater_loop">
        <?php while( have_rows('repeater_field') ): the_row(); $i++; ?>
            <p>This is row number <?php echo $i; ?>.</p>
            <!-- call your sub_fields as needed -->
        <?php endwhile; ?>
        </div>
    <?php endif; ?>
    

    This would output a div with a paragraph tag that displays your row number.