WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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?


share|improve this question
Woah there - tame that unclosed <li>! Would selecting #servicelist li:last-child not be easier? – TheDeadMedic Jul 23 '12 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 Jul 24 '12 at 8:31

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>';
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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