WordPress的各分类页面如何调用其子分类?
让每个大分类内页的side里显示子分类
在很多情况下,我们都会在进入一个分类后,希望在这个页面的导航中显示其子分类,效果如下图:

如果分类ID是固定的,用<?php wp_list_cats(‘child_of=5′); ?>就可以实现,但是如果分类ID不是确定的,而是希望自动绑定各分类,这个函数就不能满足了,需要先获取到当前页面的根分类ID,下面的函数就可以实现:
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号
}
$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 wp_list_cats(‘child_of=’ . get_category_root_id($cat) . ‘&depth=0&hide_empty=0′);?>



友情链接:
调用的时候报错
[回复]
BadJohnny 回复:
五月 15th, 2011 at 12:53 下午
具体什么错误,你是不是直接复制过去的,最好检查一下分号、引号是不是英文的
[回复]
小泥巴 回复:
六月 9th, 2011 at 3:31 上午
已经解决了,谢谢你
[回复]
请教下,如何获取该栏目以及子栏目的十篇文章,不是每个栏目十篇~~要自动判断的那种,不是指定cat ID的那种~~
[回复]
BadJohnny 回复:
七月 6th, 2011 at 7:46 下午
不好意思,没大理解。
[回复]
怎么用??
[回复]
BadJohnny 回复:
十二月 12th, 2011 at 1:51 上午
把function get_category_root_id($cat) 这段函数放入functions.php中,然后在需要显示二级分类的地方,加入
[回复]