获取Wordpress分类标准的根术语ID

时间:2012-10-11 20:26:59

标签: wordpress wordpress-theming taxonomy

我在archive.php页面上,我想找到分层分类法的根父级的术语ID。这样我就可以打印出与那个父词相关的所有内容。

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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);
?>