Reputation: 4995
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
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
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