Skip Navigation

[Resolved] Add a condition to check whether on parent taxonomy archive

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

Problem:
Client has a hierarchical taxonomy and only wants to show something on parent term archive pages, not child term archive pages. What test can be included in a wpv-conditional shortcode?

Solution:
You would need to register a custom function that checks whether the current term for the taxonomy archive being viewed has parents or not, which can then be used as the conditional in a wpv-conditional shortcode (after registering the custom function), e.g.

function tssupp_archive_term_is_parent(){
 
    global $wp_query;
    $tax = $wp_query->get_queried_object();
 
    return $tax->parent == 0;
 
}

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

This support ticket is created 6 years, 4 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+00:00)

Author
Posts
#915232

We want filter to show filter control only in parent taxonomy archive pages and not in child archive pages.

For example, we have created a filter for taxonomy archive page and want that filter to be shown

only in: example.com/parent-taxonomy/

but not in: example.com/parent-taxonomy/child-taxonomy

#915252

Nigel
Supporter

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

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

For that you would need to add some custom code that checks whether the current archive term is a top-level (parent) term or not.

I added the following code to the functions.php file of my test site:

function tssupp_archive_term_is_parent(){

    global $wp_query;
    $tax = $wp_query->get_queried_object();

    return $tax->parent == 0;

}

I then registered it to be able to use it in conditional shortcodes at the page Toolset > Settings > Front-end content (https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/).

Then in the Search and Pagination section of my custom archive I added the following conditional shortcode:

[wpv-conditional if="( tssupp_archive_term_is_parent() eq '1' )"]
<p>Only show this for parent terms</p>
[/wpv-conditional]
#915578

Thank you so much Nigel, code is working.