Style last item with a different class in a loop

This example styles the last item in a loop with a different list class in an Advanced Custom Field ( acf ) repeater in ACF Options page. It allows you to add as many emails as you want with the last one not receiving the class that gives the border or pipe in the list.

<?php 
	// get the count
	$last = count(get_field('emails','option'));
	// loop through repeater
		if(get_field('emails','option')) :  ?>
           <?php $x=1; while(has_sub_field('emails','option')) : ?>
            
       <?php 
			// if is not the last list item
			if($x != $last) {
				$islast = 'not-last';
			} else { // else it is the last list item
				$islast = 'last';
			} ?>
            
           <li class="<?php echo $islast; ?>"> 	
				<?php $email = get_sub_field('email', 'option'); ?>
               <a href="mailto:<?php echo antispambot($email); ?>">
                    <?php echo antispambot($email); ?>
               </a>
           </li>
         
         <?php $x++; endwhile; // count up x and end while loop ?>
       <?php endif; ?>

 

Leave a Reply

Your email address will not be published. Required fields are marked *

16 + = 22