Skip Navigation

[Resolved] Removing the last comma from a comma separated list of posts titles in Views

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to output a comma-separated list using a View but not having a trailing comma after the last item.

Solution:
You need to output the content followed by a comma for most of the loop iterations except the last one, where you would output the content only.

You output different content for different iterations using the wpv-item shortcode.

This is handled automatically if you use the Loop Wizard and choose the comma-separated list output option.

Relevant Documentation:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-item

100% of people find this useful.

This support ticket is created 5 years, 11 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.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by justinG-2 5 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#1168921

I have two post types - solutions and usages setup in a many to many post relationship.

I have created a view where I am showing details about both post types including the categories they are in and the related posts. So the output on the front end is something like:

Post Title
Categories: Category 1, Category 2, Category 3
Related Solutions: Solution 1, Solution 2, Solution 3,

The list of related posts is produced from a view with a query filter with the options: Select items in the Solution Usages relationship as related items of the current post in the loop.

For the loop I created a content template that simply outputs the title and a comma like this: [wpv-post-title], as there is no option in the query filter to output an array/list of comma separated post titles.

However my issue is that when I display the related solutions a comma appears at the end of the list (as seen above) unlike the categories where an option is given in the dialog box for inserting [wpv-post-taxonomy ] to choose a separator.

Is it possible to use the views conditional shortcode to create a conditional statement that checks if there is one post title not to place a comma but if there is more that one to create a comma separated list?

#1169444

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Justin

We recently added a new output format to Views for just this purpose.

In your View use the Loop Wizard for the output section and choose the final option "List with separators".

That will generate the Loop Output section markup in the format required to produce a list separated by a comma (default).

#1169776

So the key here is the [wpv-item] shortcode... Thanks, it works perfectly!