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
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
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.
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
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.
Thanks Christian...I'll perform some tests and weigh up how important form is over function