在WordPress获取父分类是比较常见的,主题君今天给大家讲讲怎么获取父分类的所有子分类。
其实也很简单,主要用到这个函数:wp_list_categories(),不过在用函数之前需要添加一段代码:
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号
}
把上面代码添加到主题的 function.php 文件最下,然后在需要获取父分类的所有子分类的地方使用下面代码:
<?php
$cats = get_categories(array(
'child_of' => $djcatid,
'parent' => $djcatid,
'hide_empty' => 0
));
if(!empty($cats)){
foreach($cats as $the_cat){
echo '<li><a href="'.get_category_link($the_cat).'" rel="external nofollow" class="sl1 slli">'.$the_cat->name.'</a></li>';
}
}
?>
这样就可以在获取到父分类的所有子分类
文章链接:https://www.jitheme.com/2655.html
更新时间:2024年03月28日
1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:service@jitheme.com,我们将第一时间处理!
2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。
3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读极(子)主题网络服务协议。