Support

Account

Home Forums Add-ons Repeater Field Repeater – "while" detect last

Helping

Repeater – "while" detect last

  • Hi,
    I use a repeater and want to have after every item a backslash. But how I detect the last item? Here I posted the code. Thanks for all help.

    Alwin

    <?php if( have_rows('services_repeater', $services) ): ?>
    
    									
    <?php while( have_rows('services_repeater', $services) ): the_row(); 
    
    										// vars
    										$service = get_sub_field('service');
    										$description = get_sub_field('description');
    ?>
    		
    										<span class="hint--bottom" data-hint="<?php echo $description; ?>"><?php echo $service; ?></span> <div id="separator"> / </div>
    											
    
    <?php endwhile; ?>
    <?php endif; ?>
  • Treat the repeater field as an array, you can count the number of values.

    <?php if( have_rows('services_repeater', $services) ): ?>
    	<?php $rowCount = count( get_field('services_repeater', $services) ); //GET THE COUNT ?>
    	<?php $i = 1; ?>
    	<?php while( have_rows('services_repeater', $services) ): the_row(); ?>
    
    		<?php // vars
    			$service = get_sub_field('service');
    			$description = get_sub_field('description');
    		?>
    
    		<span class="hint--bottom" data-hint="<?php echo $description; ?>"><?php echo $service; ?></span>
    		<?php if($i < $rowCount): ?>
    			<div id="separator"> / </div>
    		<?php endif; ?>
    		<?php $i++; ?>
    	<?php endwhile; ?>
    <?php endif; ?>
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.