Skip Navigation

[Resolved] Treating last item in repeating field differently

This support ticket is created 5 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 1 reply, has 2 voices.

Last updated by Diego Pereira 5 years, 10 months ago.

Assisted by: Diego Pereira.

Author
Posts
#1188215

Tell us what you are trying to do?
I would like to iterate through a repeating field, and treat the last item in the list differently than the rest of the items. Basically, I need to output a comma-separated list, but, before the final item, I need to output the word "and". So, if there is only 1 item in the list, it would output just the text of the item (e.g. "Item 1"). If there are two items in the list, it would output something like "Item 1 and Item 2". If there are 3 items in the list, it would output something like "Item 1, Item 2 and Item 3".

Is there a way for me to do this?

Is there any documentation that you are following?
I did look at the documentation for the wpv-for-each shortcode (https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-for-each) and the documentation for Repeating Fields (https://toolset.com/documentation/user-guides/repeating-fields/), but I didn't see any way to accomplish this there.

Is there a similar example that we can see?
No

What is the link to your site?
hidden link

#1188305

Diego Pereira
Supporter

Hello @curtissG, welcome to the Toolset support Forum!

I think this can be achieved using CSS + Pseudo Selectors.

FIrst, you need to wrap each item into a span, then use CSS to add the commas to each item. After that you should be able to remove the comma from the first item using the "first-child" and "last-child selectors"

Please take a look at this example: hidden link

All the best,
Diego