Support

Account

Home Forums Add-ons Repeater Field Repeater – Only Display First 3 Rows

Solved

Repeater – Only Display First 3 Rows

  • Hello,

    I have an instance where I want to only display the first 3 rows of my repeater, below is my code – is anyone able to help out? Thank you!

    <?php
    if( have_rows('latest_offers', 'options') ):
     
        while ( have_rows('latest_offers', 'options') ) : the_row(); ?>
    
    		<a href="<?php the_sub_field('page_link'); ?>" class="hot">
    			<div class="img"><img src="<?php the_sub_field('brand_logo'); ?>" alt=""></div>
    			<div class="text">
    				<div class="smlltxt"><?php the_sub_field('heading'); ?></div>
    				<div class="num"><?php the_sub_field('price'); ?></div>
    				<div class="txt"><?php the_sub_field('description'); ?></div>
    			</div>
    			<img src="<?php echo get_template_directory_uri(); ?>/img/hot.png" alt="" class="hot_img">
    		</a>
    	
        <?php endwhile;
     
    else :
    
    endif;
    ?>
  • Use a counter and break out the loop when you get to ‘3’.

    <?php
    if( have_rows('latest_offers', 'options') ):
     	$i = 0;
        while ( have_rows('latest_offers', 'options') ) : the_row(); ?>
    		<?php $i++; ?>
    		<?php if( $i > 3 ):
    			<?php break; ?>
    		<?php endif; ?>
    		
    		<a href="<?php the_sub_field('page_link'); ?>" class="hot">
    			<div class="img"><img src="<?php the_sub_field('brand_logo'); ?>" alt=""></div>
    			<div class="text">
    				<div class="smlltxt"><?php the_sub_field('heading'); ?></div>
    				<div class="num"><?php the_sub_field('price'); ?></div>
    				<div class="txt"><?php the_sub_field('description'); ?></div>
    			</div>
    			<img src="<?php echo get_template_directory_uri(); ?>/img/hot.png" alt="" class="hot_img">
    		</a>
    	
        <?php endwhile;
     
    else :
    
    endif;
    ?>
  • For anyone stumbling on this answer, please be aware that there’s an unclosed php tag in the snippet provided. Here’s the updated code

    <?php
    if( have_rows('latest_offers', 'options') ):
     	$i = 0;
        while ( have_rows('latest_offers', 'options') ) : the_row(); ?>
    		<?php $i++; ?>
    		<?php if( $i > 3 ): ?>
    			<?php break; ?>
    		<?php endif; ?>
    		
    		<a href="<?php the_sub_field('page_link'); ?>" class="hot">
    			<div class="img"><img src="<?php the_sub_field('brand_logo'); ?>" alt=""></div>
    			<div class="text">
    				<div class="smlltxt"><?php the_sub_field('heading'); ?></div>
    				<div class="num"><?php the_sub_field('price'); ?></div>
    				<div class="txt"><?php the_sub_field('description'); ?></div>
    			</div>
    			<img src="<?php echo get_template_directory_uri(); ?>/img/hot.png" alt="" class="hot_img">
    		</a>
    	
        <?php endwhile;
     
    else :
    
    endif;
    ?>
  • I have the below in a WordPress theme file template part, called from page.php
    The use case is, my ACF field set is used on Page screens as a page content creator.

    The template files should spit out content. In the below case, it is a social-proof section in Bootstrap mark-up, comprising text fragment and logos.
    This works fine when I display all logos, but there are too many for display on this section of the page. So (using this thread as a guide), I tried limiting this to six logos. That part works fine.

    The weird thing is, it leads to a single, secondary repetition of the whole container, immediately beneath, just without any of the content – no logos_tee-up text, no logos files, just the mark-up.

    I’m confused, because there is actually one instance of the social_proof row. Can’t understand why it has been repeated once and once only.

    When I remove the iteration and break, the repeated container doesn’t happen. But I do need to limit. What is not right here?

    <?php if ( have_rows( 'social_proof' ) ) : ?>
    	<?php while ( have_rows( 'social_proof' ) ) : the_row(); ?>
    
        <div class="container-fluid bg-white p-4 px-md-5 pt-md-4 pb-md-5">
    
          <p class="text-muted"><?php the_sub_field( 'logos_tee-up' ); ?>:</p>
    
          <div class="row">
    
            <?php if ( have_rows( 'logos' ) ) : ?>
              <?php $i = 0; ?>
        			<?php while ( have_rows( 'logos' ) ) : the_row(); ?>
                <?php $i++; ?>
                		<?php if( $i > 6 ): ?>
                			<?php break; ?>
                		<?php endif; ?>
        				<?php $logo_image = get_sub_field( 'logo_image' ); ?>
        				<?php if ( $logo_image ) { ?>
        					<div class="col-4 col-sm-3 col-md-2 col-lg-2 col-xl-2 align-self-center  py-3"><img class="img-fluid" src="<?php echo $logo_image['url']; ?>" alt="<?php echo $logo_image['alt']; ?>"></div>
        				<?php } ?>
        			<?php endwhile; ?>
        		<?php else : ?>
        			<?php // no rows found ?>
        		<?php endif; ?>
    
          </div>
    
        </div>
    
      <?php endwhile; ?>
    <?php endif; ?>
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.