2012-10-11 50 views

回答

0

沒有必要創建一個自定義循環。只需使用Get Ancestors

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

您可以試試:

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; 
相關問題