wordpress获取当前分类下的子分类

admin wordpress评论598字数 526阅读模式
摘要

前面有和大家分享过,wordpress怎么获取当前页面的子页面,那么同样的道理,分类下的子分类也是可以直接调用的,那么怎么样才能调用当前分类下的子分类呢?下面再来给大家分享一下wordpress如何获取当前分类下的子分类。

一、首先在functions.php函数文件中添加以下函数:

  1. <?php  
  2. function get_category_root_id($cat)  
  3. {  
  4. $this_category = get_category($cat);   // 取得当前分类  
  5. while($this_category->category_parent) // 若当前分类有上级分类时,循环  
  6. {  
  7. $this_category = get_category($this_category->category_parent); // 将当前分类设为上级分类(往上爬)  
  8. }  
  9. return$this_category->term_id; // 返回根分类的id号  
  10. }  
  11. ?>  

二、在需要调用当前分类下的子分类的地方插入以下代码即可:

  1. <?php  
  2. wp_list_categories("child_of=".get_category_root_id(the_category_ID(false)).  
  3. "&depth=0&hide_empty=0&title_li=");  
  4. ?>  

这里要注意的是,在插入functions.php函数时需要看清楚PHP的开始和结束标签。

版权声明:文章图片资源来源于网络,如有侵权,请留言删除!!!
admin
  • 本文由 发表于 2021年9月29日 17:14:20
  • 转载请务必保留本文链接:https://www.58pxe.com/7812.html
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: