Talon
Talon

Reputation: 4995

Getting the Root Term ID of a Wordpress Taxonomy Page

I'm on the archive.php page and I want to find the term ID for the root parent of a hierarchical taxonomy. That way I could print out everything related to that parent term.

Upvotes: 0

Views: 1634

Answers (2)

maiorano84
maiorano84

Reputation: 11971

There's no need to create a custom loop. Just use Get Ancestors:

<?php
$cat = get_query_var('cat'); //Only works if on Archive page
$ancestors = get_ancestors($cat->term_id, 'category');
$root = end($ancestors);
?>

Upvotes: 1

Samuel Cook
Samuel Cook

Reputation: 16828

You might try:

global $wp_query;
$cat_obj = $wp_query->get_queried_object();
$this_cat = $cat_obj->term_id;
$this_cat = get_category($this_cat);
if($this_cat->category_parent!=0){
    do{
        $this_cat = get_category($this_cat->category_parent);
    }while($this_cat->category_parent!=0);
}
$root_term = $this_cat->term_id;

Upvotes: 0

Related Questions