调用WordPress父子分类目录

将下面代码加到主题function.php模板文件中:

function get_category_root_id($cat)
{
    $this_category = get_category($cat); // 取得当前分类
    while($this_category->category_parent) // 若当前分类有上级分类时,循环
    {
        $this_category = get_category($this_category->category_parent); // 将当前分类设为上级分类(往上爬)
    }
    return $this_category->term_id; // 返回根分类的id号
}

调用显示代码加到主题模板的适当位置:

<?php
    if(is_category())
    {
        if(get_category_children(get_category_root_id(the_category_ID(false)))!= "" )
        {
            echo '<ul>';
            echo wp_list_categories("child_of=".get_category_root_id(the_category_ID(false)). "&depth=0&hide_empty=0&title_li=&orderby=id&order=ASC");
            echo '</ul>';
        }
    }
?>

相关链接:http://zmingcx.com/wordpress-sons-category.htmlhttp://www.sjyhome.com/wordpress/wp-root-id.html