Skip Navigation

[Resolved] Only show template’s grouped section if it matches a specific taxonomy term

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

Problem: I would like to hide a grouped section of my template if the post has a specific term in the "Exhibitor Type" taxonomy.

Solution: Use conditional blocks and the has_term function built into WordPress to test for specific term slugs.

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.

Our next available supporter will start replying to tickets in about 5.85 hours from now. Thank you for your understanding.

This topic contains 2 replies, has 2 voices.

Last updated by michaelR-26 4 months, 3 weeks ago.

Assigned support staff: Christian Cox.

Author
Posts
#1705559

Currently, I have one custom template that I am using for a custom post type of Exhibitor. In the template, I have a grouped section that I would only like shown on the front end if the post is assigned a particular taxonomy term - in this case the term is "Resorts & Travel". Othewise, the grouped section would be hidden or removed.

Can you tell me how to do this? I have assigned the grouped section the class name of "resorts-travel"

I am trying to avoid making new custom post types to replace each of the taxonomy terms I am currently working with. Currently there is on Taxonomy "Exhibitor Type", with three terms, Resorts & Travel, Shopping Mall and Transportation.

#1705801

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-05:00)

Hi, you can use Toolset's Conditional Blocks in the Block Editor to achieve conditional display based on whether or not a post has a specific term from a specific taxonomy applied.

First, go to Toolset > Settings > Front-end Content and register the function has_term in the section "Functions inside conditional evaluations". This will allow you to use the WordPress function has_term ( function documentation here: https://developer.wordpress.org/reference/functions/has_term/ ) in your conditional settings. Then insert a Toolset Conditional block in the Block Editor and choose "Custom Function" in the first input. Then select the custom function has_term from the list of available functions.

Next you need to fill in the parameters field below the function name. Three parameters are required: the slug of the term, followed by the slug of the taxonomy, followed by the post ID. Use commas to separate each parameter. See the attached screenshot conditional.png for an example.

You can find the term slug in the wp-admin list of terms for the Exhibitor Type taxonomy, and you can find the taxonomy slug in Toolset > Taxonomies. You will use the wpv-post-id shortcode in the third parameter to represent the dynamic post ID (different for each post). So if the Resorts & Travel term slug is "resorts-travel" and the Exhibitor Type taxonomy slug is "exhibitor-type", then you would enter in the Parameters field:

resorts-travel,exhibitor-type,[wpv-post-id]

Once you've set the parameters, in the middle column field, leave "=" selected. In the right column, select "Static Value" and enter "1" in the input field. Click "Apply" to apply this conditional, and you will be returned to the Block Editor. Now drag your grouped section into the outlined conditional block border to complete the conditional display.

More details in the documentation here: https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/#using-custom-functions

Let me know if you run into problems implementing the conditional block and I can take a closer look.

#1705901

My issue is resolved now. Thank you!