在制造wordpress主题的时刻常常碰到怎样在wordpress分类页显现当前分类下的子分类或许在文章页显现所属分类的子分类如许的问题,特别在做中文企业主题的时刻必需要用到这个技能的。今天和人人分享之前我做企业主题时挪用子分类的函数。
1.如今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号
}
2.然后在页面要显现二级分类的处所粘贴下面这段代码即可
<?php wp_list_categories('child_of=' . get_category_root_id($cat) . '&depth=1&hide_empty=0&hierarchical=1&optioncount=1&title_li=');?>
这个函数的功用就是在分类页和文章页显现当前分类的子分类(二级分类)。

评论列表