Our Blog

How can i get only bottom level terms of a taxonomy or category in views?

How can i get only parent terms of a taxonomy ?

This is a very popular question asked in many WordPress forums and Toolset Support and as a result, i am going to write a code snippet.

Lets assume, that the depth of taxonomy hierarchy is 3 and is explained by the following example of countries, provinces and states.

  • Australia ( Parent)
    • Queensland (Child)
      • Brisbane ( Child of a child)

CODE SNIPPED TO FETCH TOP LEVEL OR PARENT TAXONOMY TERM

Copy and paste the below snipped in your current active theme functions.php file. Then replace $location variable to your current taxonomy name and then use the shortcode [ds-parent] inside the loop view of any post type template.

add_shortcode('ds-parent', 'ds_top_level_terms');
function ds_top_level_terms() {
    $locations = 'test';
    $terms = get_the_terms(get_the_ID(), $locations);
    foreach ($terms as $term) {
        $parent = $term->parent;
        if ( $parent == '0' ) {
            $term_link = get_term_link($term, $locations);
            return $term->name;
        }
    }
}

CODE SNIPPED TO FETCH MID LEVEL TAXONOMY TERMS

  • Australia ( Parent)
    • Queensland (Child)
      • Brisbane ( Child of a child)

Here again, replace $location variable to your current taxonomy name and then use the shortcode [ds-middle-terms] inside the loop view of any post type template.

function ds_sort_terms(Array &$cats, Array &$into, $parentId = 0)
{
    foreach ($cats as $i => $cat) {
        if ($cat->parent == $parentId) {
            $into[$cat->term_id] = $cat;
            unset($cats[$i]);
        }
    }
    foreach ($into as $topCat) {
        $topCat->children = array();
        ds_sort_terms($cats, $topCat->children, $topCat->term_id);
    }
}
add_shortcode('ds-middle-terms', 'ds-middle-terms');
function ds-middle-terms( $atts ) {
    extract( shortcode_atts( array(
        'taxonomy' => 'abc'
    ), $atts ) );
  
 
$categories=get_the_terms(get_the_ID(), $taxonomy);

$categoryHierarchy = array();
ds_sort_terms($categories, $categoryHierarchy);
	$amiarray = array_values($categoryHierarchy);
	foreach($amiarray[0]->children as $final){
		return $final->name;
	}    
}

CODE SNIPPED TO GET BOTTOM LEVEL TERMS OF A HIERARCHY

  • Australia ( Parent)
    • Queensland (Child)
      • Brisbane ( Child of a child)

In the same way, replace $location variable to your current taxonomy name and then use the shortcode [ds-bottom-level] inside the loop view of any post type template.

add_shortcode('ds-bottom-level', 'ds_bottom_terms');
function ds_bottom_terms() {
   extract( shortcode_atts( array(
        'taxonomy' => 'abc'
    ), $atts ) );
  
 
$categories=get_the_terms(get_the_ID(), $taxonomy);

$categoryHierarchy = array();
sort_terms_hierarchicaly($categories, $categoryHierarchy);
	$amiarray = array_values($categoryHierarchy);
	
	foreach($amiarray[0]->children as $final){
		foreach($final->children as $finall){
			return $finall->name;
		}
		}
}

Now you can use above shortcodes in a post view loop of Toolset and can only show desired term levels.

If you need further assistance, write me down in the comments section or alternatively, you can contact me through my contact page of the website.

Cheers

1 thought on “How can i get only bottom level terms of a taxonomy or category in views?”

  1. Hi. I’ve been looking for exactly this in regards to Toolset. So thanks. However I’m having an issue. For the ds-middle-terms, as soon as I add that code into my functions.php, it crashes WordPress. Second, for ds-middle-terms you say “replace $location variable to your current taxonomy name” but there is no $location variable in this code. Only in the parent. Is that what you are referring to? When trying to use it, if I add the [ds-parent] to a term that is a parent term already, it shows up. If I add it to a middle term, it doesn’t. Seems strange it would do that.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top