Support

Account

Home Forums General Issues Replace class name of last element in a repeater

Solved

Replace class name of last element in a repeater

  • I am trying to replace a class name to only the last element in a repeater.

    Here is what I have to display the repeater.

    <?php while(the_repeater_field('images', 'videoloc_14')): ?>
    <?php $image = wp_get_attachment_image_src(get_sub_field('image'), 'full'); ?>
    <a href="<?php the_sub_field('image_link'); ?>" target="_blank"><img class="class-name-to-replace" src="<?php echo $image[0]; ?>" /></a>
    <?php endwhile; ?>
    <?php endif; ?>
  • Hi @avexdesigns

    I’m guessing the last class is so you can apply a specific margin?

    You can use a counter like so:

    
    <?php 
    
    $i = 0;
    $total = count( get_field('images', 'videoloc_14') );
    
    while(the_repeater_field('images', 'videoloc_14')): $i++; ?>
    	<?php $image = wp_get_attachment_image_src(get_sub_field('image'), 'full'); ?>
    	<a <?php if($i == $total): ?>class="last"<?php endif; ?> href="<?php the_sub_field('image_link'); ?>" target="_blank"><img class="class-name-to-replace" src="<?php echo $image[0]; ?>" /></a>
    	<?php endwhile; ?>
    <?php endif; ?>
    
  • Yes. For a margin. Which I’m not happy about 🙁 But thank you!! Ill try to apply.

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

The topic ‘Replace class name of last element in a repeater’ is closed to new replies.