Support

Account

Home Forums Add-ons Repeater Field Show rows in 2 sections – offset 10 rows

Helping

Show rows in 2 sections – offset 10 rows

  • I’m trying to split the rows into 2 sections, the first showing the first 10 rows & the second showing the rest. I want to have 2 different loops so I can add something between the two.

    At the moment my code shows the first 10 correctly & then it shows the second lot but nothing I seem to be changing in the second loop is having an effect. So it’s skipping the 11th element and then showing the rest.

    <?php 
    					if( have_rows('best_braai_buys',$page_id) ): 
    					$i = 0;
    				?>
    				    <ul class="group">
    				    <?php 
    				    	while( have_rows('best_braai_buys',$page_id) ): the_row();
    				    	$i++;
    			
    						if( $i > 10 )
    						{
    							break;
    						}
    				     ?>
    				        <li class=" list-<?php echo $i; ?>">
    								<img src="<?php the_sub_field('product_image'); ?>" alt="<?php the_sub_field('product_description'); ?>" />
    								<h6><?php the_sub_field('product_price_rand'); ?><sup><?php the_sub_field('product_price_cents'); ?></sup></h6>
    								<?php the_sub_field('product_description'); ?>
    
    					        	
    							<?php if(get_sub_field('product_link')) { ?>
    					        	<a class="btn2" href="<?php the_sub_field('product_link'); ?>" target="_blank">BUY NOW <span class="fa fa-chevron-right"></span></a>
    					        <?php } ?>
    				        </li>
    				    <?php endwhile; ?>
    				    </ul>
    				
    				<?php 
    					endif; 
    				?>
    <!--second loop-->
    <?php 
    					
    					if( have_rows('best_braai_buys',$page_id) ): 
    					$ii = 0;
    				?>
    				    <ul class="group">
    				    <?php 
    				    	while( have_rows('best_braai_buys',$page_id) ): the_row();
    				    	$ii++;
    			
    						if ($ii != 0) {
    				     ?>
    				        <li class=" list-<?php echo $ii; ?>">
    								<img src="<?php the_sub_field('product_image'); ?>" alt="<?php the_sub_field('product_description'); ?>" />
    								<h6><?php the_sub_field('product_price_rand'); ?><sup><?php the_sub_field('product_price_cents'); ?></sup></h6>
    								<?php the_sub_field('product_description'); ?>
    
    					        	
    							<?php if(get_sub_field('product_link')) { ?>
    					        	<a class="btn2" href="<?php the_sub_field('product_link'); ?>" target="_blank">BUY NOW <span class="fa fa-chevron-right"></span></a>
    					        <?php } ?>
    				        </li>
    				    <?php 
    						}		
    					    endwhile; ?>
    				    </ul>
    				
    				<?php endif; ?>
  • the most useful way (imho) would be: fill values into variables and echo than when needed.

    <?php
    if( have_rows('best_braai_buys',$page_id) ): 
    $i = 0;
    $before_best_braai_buys = '<ul class="group">';
    $after_best_braai_buys = '</ul>';
    $first10_best_braai_buys = ''; //variable for first 10products
    $morethan10_best_braai_buys = ''; //variable for rest of products
    			    
    	while( have_rows('best_braai_buys',$page_id) ): the_row();
    	$i++;
    	$bbb_product_image = '';
    	$bbb_product_description = '';
    	$bbb_product_image = get_sub_field('product_image');
    	$bbb_product_description = get_sub_field('product_description');
    		if( $i > 10 ) {
    			$morethan10_best_braai_buys .=  '<li class=" list-'.$i.'">';
    			$morethan10_best_braai_buys .= '<img src="'.$bbb_product_image.'" alt="'.$bbb_product_description.'" />';
    // adapt/extend with rest of your values ... important => no echo or nonphpcode
    
    			$morethan10_best_braai_buys .= '</li>';
    		} else {
    			$first10_best_braai_buys .=  '<li class=" list-'.$i.'">';
    			$first10_best_braai_buys .= '<img src="'.$bbb_product_image.'" alt="'.$bbb_product_description.'" />';
    // adapt/extend with rest of your values ... important => no echo or nonphpcode
    
    			$first10_best_braai_buys .= '</li>';
    			}
    	endwhile; 
    endif;
    
    //now you can echo everything you like with something between
    
    //first 10 products
    echo $before_best_braai_buys;
    echo $first10_best_braai_buys;
    echo $after_best_braai_buys;
    
    //whatever you wish
    echo $somethingbetween;
    
    //rest of products
    echo $before_best_braai_buys;
    echo $morethan10_best_braai_buys;
    echo $after_best_braai_buys;
    
    ?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Show rows in 2 sections – offset 10 rows’ is closed to new replies.