WordPress获取当前分类的子分类
在制作WordPress主题的时候经常遇到怎么在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;
}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>’;
}
?>这个函数的功能就是在分类页和文章页显示当前分类的子分类(二级分类)。
CSS样式:
.catleader .cat-item {
position: relative;
display: inline-block;
border: 2px solid #d9d9d9;
padding: 0 .8rem;
line-height: 1.85rem;
border-radius: .2rem;
color: #595959;
background: #fff;
user-select: none;
}2.然后在页面要显示二级分类的地方粘贴下面这段代码即可CSS样式:
类别:WordPress技巧、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论功能已经关闭!