Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

How best do a loop with the results separated by a delimiter

Is there an easy way to place delimiters (/) in between list items like:

item1 / item2 / item3 / item4 / item5

    <?php if(get_field( 'pro_test' ) ): ?>
        <ul>
        <?php while( has_sub_field( 'pro_test' ) ): ?>
            <li><?php the_sub_field( 'pro_single' ); ?></li><span>/</span>
        <?php endwhile; ?>
        </ul>
    <?php endif; ?>

Problem would be that in that case the last item would have a / too

item1 / item2 / item3 / item4 / item5 /

Is there an easy and clean way to prevent the / on the last item? In the present example i use the advanced custom field repeater field. Thanks Ralf

3 Answers

Matt Campbell
Matt Campbell
9,767 Points

I'd use CSS to add the delimiter and then last-child to remove it from the last item in the list.

Use CSS content

Argh I was head blocked. Indeed the easiest fix for the problem. Thanks!