Skip Navigation

[Resolved] Display Conditionally if Term Assigned to Taxonomy using Blocks

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

Problem: I would like to display some content conditionally if any term from a specific taxonomy is assigned to the current post.

Solution: Use a conditional block and turn on the advanced condition editor. Add the following condition:

('[wpv-post-taxonomy type="book-tax" format="slug" ]' ne '')

Replace book-tax with the slug of the taxonomy.

This support ticket is created 3 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.

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 5 replies, has 2 voices.

Last updated by JoelK2744 3 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1968919

I am creating a content template and I would like to conditionally display a section if at least one term has been assigned to a taxonomy. I can see this documentation - https://toolset.com/documentation/legacy-features/views-plugin/displaying-taxonomies-conditionally/#one - for the old style views, but I would like to accomplish this with the conditional block.

So in summary I have a Taxonomy called 'Facilities', if the post has at least one facility assigned I would like to display something like this...

FACILITIES (heading)
> Facility 1
> Facility 2
...etc

But if there is no facility assigned, I don't want anything displayed (including the 'FACILITIES' heading).

Thanks

#1968967

Actually I think I've found a way to do this using a taxonomy view within the content template...is that the best method?

Thanks

#1969623
Screen Shot 2021-02-28 at 10.03.38 AM.png

Actually I think I've found a way to do this using a taxonomy view within the content template...is that the best method?
That is a valid option, yes, but since it is not currently possible to create taxonomy-based Views in the Block Editor, it might not be the most straightforward method for all Users. Many Users do not have the legacy editor active, and would not know how to create a taxonomy View. I think the simplest Blocks-based method is to use a conditional block in the block editor, and then use the advanced conditional editor to insert code similar to the documentation example:

('[wpv-post-taxonomy type="book-tax" format="slug" ]' ne '')

Replace book-tax with the slug of the Facilities taxonomy. Screenshot attached.

Then you can drag whatever content you want to display conditionally into the Conditional Block, including the Facilities header and list of Facilities terms.

#1970357

That's great, thanks Christian, I've got that working.

The view approach gives me a bit more control over the formatting of the facilities list (e.g. I've added an svg icon before each item and set each item to display inline-block), but I'm just wondering whether there are any performance implications with having views within a template compared to using the single field block? When I go to Toolset>Views in the dashboard, the view is showing as 'not cached'.

Thanks again

#1970741

I'm just wondering whether there are any performance implications with having views within a template compared to using the single field block?
Yes, the different approaches will require different queries and therefore there are performance implications. It's certainly acceptable to have a View in a template, so that's nothing inherently wrong about that approach. If you nest other features inside the loop, like another View that queries posts, or a Types termmeta image field that requires a custom image size, then you'll get compounded performance hits during loops of loops. It is not really possible for me to predict exactly how much difference there will be between the two approaches because of variables like the final View output / contents, other content on the page, site database size, server configuration, and traffic load, so I suggest trying it both ways, and calculate an average of multiple performance tests in each scenario. You can turn on a plugin like Query Monitor to get a precise indication of the different queries required in each scenario, and you can use a site like https://www.webpagetest.org/ to get a front-end performance rundown.

#1970939

Thanks Christian...I'll perform some tests and weigh up how important form is over function