Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
6  
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

1 Answer 1

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

 
discard

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.