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; ?>
    • Elliot

    • December 11, 2013 at 10:45 am

    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)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.