0

this one has been getting me for a while. At the moment I have a repeater field with Advanced Custom Fields:

<ul id="servicelist" class="clearfix">
<?php if(get_field('homepage_service')): ?>
        <?php while(the_repeater_field('homepage_service')): ?>
            <li><img src="<?php the_sub_field('service_image'); ?>" />
        <?php endwhile; ?>
<?php endif; ?>
</ul>

What I'm trying to do is apply a class to the last item generated by this... is there a way (or a function) to give a class to every last list item on a WP site?

Thanks

2
  • 7
    Woah there - tame that unclosed <li>! Would selecting #servicelist li:last-child not be easier? Commented Jul 23, 2012 at 9:48
  • Easier, in a way, yes - but if I can get it done via PHP then I won't have to worry about browser compatibility
    – Phill
    Commented Jul 24, 2012 at 8:31

1 Answer 1

2

I can only see achieving this by first building an array, then looping over it - I can't find anything in APC's docs about checking if it's the last iteration of the_repeater_field:

$images = array();
while ( the_repeater_field( 'homepage_service' ) )
    $images[] = get_sub_field( 'service_image' );

if ( $images ) {
    $image_last = array_pop( $images );

    echo '<ul id="servicelist" class="clearfix">';
    if ( $images )  
        echo '<li>' . implode( '</li><li>', $images ) . '</li>';
    echo '<li class="last-child">' . $image_last . '</li>';
    echo '</ul>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.