Skip Navigation

[Resolved] Passing values to Views loop to make a comparison

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+01:00)

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 5 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#913422

I want to create a list of taxonomies in post content template where selected taxonomies are highlighted.

I'm currently thinking adding a view for that, and shortcode in template might be something like:
[wpv-view name="my-custom-view-for-list" selectedvalues="[wpv-post-taxonomy type="name-of-taxonomy" format="slug"]"]

In the that view I´d like to add condition, which highlights the selected taxonomies.
So... is outputting all the taxonomies and comparing the taxonomy slug with passed value right way?

Is this correct way to do this or is there a better way?
And what kind of condition output should I make in the views loop?

#913443

Nigel
Supporter

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

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

Hi there

That sounds like a plan.

Pass the taxonomy terms applied to the current post as a shortcode attribute to your View.

The View queries the taxonomy terms and will display all of them.

In the Loop Output section you will have access to the passed attribute with the wpv-attribute shortcode (https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-attribute).

You can use that inside a conditional shortcode (https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-conditional).

See https://toolset.com/documentation/user-guides/passing-arguments-to-views/ and https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

In the Loop Output section of your View where you output the taxonomy terms, you will want to have a container div for those and add a class that you use to highlight those required, wrapping the class declaration in your conditional shortcode that tests whether the current term is in the list of terms passed as a shortcode attribute.

Which is where we run into a problem. The comparisons available in wpv-conditional are =, !=, <, > etc.

We have a feature request to add "CONTAINS" but it is not implemented yet.

If your posts have more than one term assigned then the shortcode attribute will be a comma separated list, so you can't directly compare the current term to this list of terms.

If that is the case (multiple terms assigned) you will need to create a custom shortcode and pass the list of terms to that, perform the comparison in PHP, and return true or false.

See here for details of creating shortcodes: https://developer.wordpress.org/plugins/shortcodes/

If you get stuck along the way, let me know.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.