wordpress获取当前分类的子分类列表的方法

WordPress教程 4747

部分仿站客户对制作的wordpress企业主题需求显示当前分类页面的子分类的功能效果,使用wordpress程序模板很容易可以解决这个问题,WP博主仅需要添加两段相应的获取子分类的函数代码即可。

一、把以下函数代码添加到当前主题的functions.php文件

1
2
3
4
5
6
7
8
9
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号
	}

二、在分类页面archive.php添加调用代码:

1
2
3
<?php
    wp_list_categories("child_of=".get_category_root_id(the_category_ID(false)). "&depth=0&hide_empty=0&title_li=");
?>

精品推荐: