Support

Account

Home Forums Add-ons Repeater Field Limiting Results from Repeater Field

Solving

Limiting Results from Repeater Field

  • I have a repeater field which appears on a specific page, as well as the footer used site wide. I would like to limit the results just in the footer, while keeping the full list of results on the main page content. Is there any way of doing this?

    This is what my code looks like now:

    <?php 
    						if(get_field('show_dates', 'option')) { ?>
    <ul>
        <?php while(the_repeater_field('show_dates', 'option')):?>
        <li><a href="<?php site_url(); ?>/show-dates"><?php the_sub_field('show_date_table_city');?></a></li>				 
        <? endwhile; ?>
    </ul>	 
    <?php }
    else { 
    echo '<p>Show dates to be announced soon.</p>';
    }?>

    Thanks

    Cory

  • Hi @xranerx

    A simple counter can be used to limit the fields. I have taken the liberty of neatening your code and adding in a limiter of 4 rows:

    
    <?php if(get_field('show_dates', 'option')):
    	
    	$i = 0;
    	
    	?>
    	<ul>
    		<?php while( has_sub_field('show_dates', 'option') ): 
    			
    			$i++;
    			
    			if( $i > 4 )
    			{
    				break;
    			}
    			
    			?>
    			<li><a href="<?php site_url(); ?>/show-dates"><?php the_sub_field('show_date_table_city');?></a></li>				 
    		<?php endwhile; ?>
    	</ul>	 
    <?php else: ?>
    	<p>Show dates to be announced soon.</p>
    <?php endif; ?>
    

    Please continue to practice your PHP and write clean code that does not have mixed styles.

    Good luck

  • What if out of those 4 you only wanted to show 3 and 4?

  • Hi Elliot,

    The break is not working for me. I am using what you have suggested.

    Cheers,

    Ben.

  • I am also having issues with this. It is limiting the number of results, but causing the page to endlessly load. I have used this in the past with no issues.

  • After a few hours of head scratching I gave up on using the standard repeater loop and instead used a foreach loop. Worked fine, even with the break in an if statement as shown here.

  • <?php
    if (have_rows(‘happy_clients’)){
    $i=0;
    while (have_rows(‘happy_clients’)){ the_row();
    $image = get_sub_field(‘happy_clients_image’);
    //if($i==4){ break; }
    //echo $i;
    ?>
    <div class=”col-sm-6 col-xs-6″>
    <div class=”partner-item mb-30″>
    ” alt=”Partner Image”>
    </div>
    </div>

    <?php $i++; if($i>3){ break; } }; }; ?>

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Limiting Results from Repeater Field’ is closed to new replies.