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.然后在页面要显示二级分类的地方粘贴下面这段代码即可

<?phpif(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>';}}?>

这个函数的功能就是在分类页和文章页显示当前分类的子分类(二级分类)。

posted on   剩余价值  阅读(671)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示