17th Nov '22
/
7 comments

[Function] Current Term Has Children

Updated on 20 Jul 2024

A user asks:

I’m working on an archive template for WooCommerce products.

I’m trying to hide a section if there are no child categories but I cannot figure out how to set the conditions to do this.

Anyone else know what term or dynamic data to use to get this to work?

We can define a custom function that uses get_term_children() to return true or false, then use it with Bricks’ Dynamic data condition to render a Section (or any element) only if the current term has children.

Add this in child theme’s functions.php or a code snippets plugin:

// Function to check if the current term of the given taxonomy has children
function bl_current_term_has_children( $taxonomy = 'category' ) {
	$term_children = get_term_children( get_queried_object_id(), $taxonomy );

	return count( $term_children ) > 0;
}

get_queried_object_id() gets the ID of the current term when viewing a term archive page. Example URL:

https://bricks.local/product-category/clothing/accessories/

We have set category as the default argument for the function.

To check for a different taxonomy, product_cat in this case:

{echo:bl_current_term_has_children(product_cat)}

Whitelist the bl_current_term_has_children function.

Ex.:

<?php 

add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'bl_current_term_has_children'
  ];
} );

You should also add other functions (native or custom) being used in your Bricks instance besides bl_current_term_has_children. This can be checked at Bricks → Settings → Custom code by clicking the Code review button.

More info on whitelisting can be found here.

Now the function is ready to be used in a term archive template like a template that applies to all WooCommerce product categories.

Apply a Dynamic data condition with the above dynamic data tag whilst checking it against being equal to 1.

Remember that 1 is true and 0 is false.

Reference

https://developer.wordpress.org/reference/functions/get_term_children/

Get access to all 626 Bricks code tutorials with BricksLabs Pro

7 comments

  • Matt CNC

    Thanks for sharing!

    Does it still work for you in 2024?

    I just tried it and the section I added the condition to is hidden on both categories with and without children :(

  • Thanks for sharing! I was looking for this particular solution.

    Does it still work for you in 2024?

    My product category has children but the moment I add the condition to bricks it hides my section with sub-categories anyway :(

  • David Demastus

    How would one do this inside of a query loop, instead of on an archive page? Like if I had a query loop on a standard page of my website. How do I check if the parent terms inside the query has children using bricks conditions?

  • Paco Castilla

    Sublime! Lo tenia hecho con código espagueti y esta función es super simple!! Me encanta!! Muchas gracias!

Leave a Reply to David Browne (Cancel Reply)

 

Related Tutorials..

Render an element having a specific HTML ID based on value of a custom field

Render an element having a specific HTML ID based on value of a custom field

This tutorial provides the steps to use Bricks' bricks/element/render filter to conditionally output an element that has the specified HTML ID based on the value…
Categories:
Conditionally Outputting Elements only for Posts that have a Specific Taxonomy Term

Conditionally Outputting Elements only for Posts that have a Specific Taxonomy Term

Using the core WordPress has_term() function for checking if the current post has a specific term of a given taxonomy.
Categories:
Pro
Conditionally outputting an element in a Query Loop based on date-type custom field

Conditionally outputting an element in a Query Loop based on date-type custom field

This Pro tutorial shows how to output an element in a Bricks' query loop only if the value of a date-type of custom field for…
Categories:
Pro
Conditional CSS Classes based on Bricks Query Count

Conditional CSS Classes based on Bricks Query Count

In a Bricks project I am currently working on, there are nested query loops - team members inside departments on a Page. Each team member…
Pro
Condition to Check if the Current Category Has At Least One Child

Condition to Check if the Current Category Has At Least One Child

Looking to render an element in the category archive Bricks template only if the category of the current category archive page is a parent? This…
Categories:
Weekday Condition in Bricks

Weekday Condition in Bricks

As of Bricks 1.7, the built-in "Weekday" condition does not work correctly. This is most likely due to it not taking the site's timezone (set…
Categories: