wordpress获取调用当前分类的子分类二级分类

作者: admin 分类: wordpress主题制作教程 发布时间: 2018-08-03 23:26

wordpress获取调用当前分类的子分类二级分类——-在制作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
if(is_single()||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>’;
}
}
?>

 

相关文章

发表评论

邮箱地址不会被公开。 必填项已用*标注

Powered by 草根站长笔记 © 2015-2020 草根站长笔记 Inc.版权所有,禁止转载