0

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

4

2 回答 2

1

无需创建自定义循环。只需使用Get Ancestors

<?php
$cat = get_query_var('cat'); //Only works if on Archive page
$ancestors = get_ancestors($cat->term_id, 'category');
$root = end($ancestors);
?>
于 2012-10-11T23:28:40.437 回答
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;
于 2012-10-11T21:20:52.140 回答