Support

Account

Home Forums Add-ons Repeater Field Loop through first 3 items of repeater

Unread

Loop through first 3 items of repeater

  • I recently needed to only show the first three items of my repeater array. See below for an example of how to do so.

    Set a variable $i as 0 outside the loop. Within the while loop have i count up by one and when it reaches the number of array items you’d like to display (in my case 3.) Then have it break.

    <?php if( have_rows('annualreports') ): ?>
    <?php $i = 0; ?>
    <ul class="reports_list">
    <?php while ( have_rows('annualreports') ) : the_row();?>
    <?php $i++; ?>
    <?php if( $i > 3 ): ?>
    <?php break; ?>
    <?php endif; ?>
    <li><a href="<?php the_sub_field('annual_report_file')?>" target="_blank"><?php the_sub_field('annual_report_name');?></a></li>
    <?php endwhile; ?>
    </ul>
    <?php endif; ?>
Viewing 1 post (of 1 total)

The topic ‘Loop through first 3 items of repeater’ is closed to new replies.