Premium add-on for Bricks Builder

Display Only Parent Terms Whose Has Children

In this tutorial, we shall learn how we only fetch those parent terms whose has children.

We need small PHP codes. With these PHP snippets, we shall fetch the parent term IDs which has children.

Open the functions.php of the Bricks child theme or drop the code in the 3rd party plugin.

Snippets

function getParentTermsHasChild() {
	$args = [
		'taxonomy' => 'product_cat',
		'parent' => 0,
		'hide_empty' => true,
	];

	$terms = get_terms( $args );
	if( $terms ) {
		$parent_ids = [];
		foreach( $terms as $term ) {
			$hasChild = get_term_children( $term->term_id, $term->taxonomy );
			if( $hasChild )
				$parent_ids[] = $term->term_id;
		}

		return $parent_ids;
	}

	return false;
}

Now we shall create the terms query in the builder editor and create our terms list. Here I used the bricks’ Accordion element and Custom Taxonomy Query loop builder(under Ultimate Query Builder Query type) from the BricksUltimate. Here is the screenshot of my bricks builder editor.

Code

<?php
  $parentTerms = getParentTermsHasChild();
  $args = [
    'taxonomy' => 'product_cat',
    'include' => $parentTerms,
    'orderby' => 'name'
  ];
  return $args;
?>

I also applied the condition logic to the loop builder element. If you have not any parent items which has children, then it will not display the empty HTML output at frontend.

Condition logic
Fills In: 

1 comment

  • Hello,

    Can this implement in the filter option in Bricks to?

    thx

    Pedro

Leave your comment

Search Tutorials

Request TutorialsFeatures Request