Skip Navigation

[Resolved] best way to manipulate content template output?

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

Problem: I have 3 custom fields that I would like to show in a post. Some fields may not contain a value in all posts. I would like to display these field values inline, separated by a comma, but I don't want to show a comma at the end of the list and I don't want to show commas for empty items. How can I filter the content template so this displays correctly?

Solution: You could use PHP to filter the Content Template using the Views API filter wpv_filter_content_template_output, but it's not much simpler or cleaner. You could try a CSS-based solution with an nth-child or first-of-type pseudo selector controlling the :before content of each element.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_content_template_output
https://stackoverflow.com/questions/27836769/comma-separated-spans-with-pure-css

This support ticket is created 5 years, 5 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by ericE-4 5 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1308771

I'm trying to format the output from a content template. This template builds a string based on the values from three separate checkbox fields, appending a comma and a space after each one. I need to remove the final ", " characters.

My template looks like this:

<span id="saltwater-freshwater">
[wpv-conditional if="( $(wpcf-saltwater) = '1' )"][types field='saltwater'][/types], [/wpv-conditional]
[wpv-conditional if="( $(wpcf-freshwater-lakes) = '1' )"][types field='freshwater-lakes'][/types], [/wpv-conditional]
[wpv-conditional if="( $(wpcf-freshwater-rivers) = '1' )"][types field='freshwater-rivers'][/types], [/wpv-conditional]
</span>

and then I added some custom JS to the JS editor as follows:

var string = jQuery("#saltwater-freshwater").html();

if (string.slice (-2) == ", " ) {
    jQuery("#saltwater-freshwater").html(string.slice(0,string.length-2));
  
} else {
    jQuery("#saltwater-freshwater").html(string);
}

This works in effectively removing any final ", ", but it seems like a pretty crude way of manipulating output from a content template or a view. Is there a more efficient way of accomplishing the same thing?

#1308881

Hi, you could filter the Content Template output with PHP, but it's not much simpler: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_content_template_output).

A conditional-based approach would be complex, too. If I wanted to do something like this, I would also consider a CSS-based approach, where I could use a nth-child or first-of-type pseudo selector to insert a comma and space :before each element except the first one. Something like this maybe: https://stackoverflow.com/questions/27836769/comma-separated-spans-with-pure-css

#1308967

OK great. I was thinking there might be a built-in function, but I'll consider the css approach--that's a great idea.