WordPress获取当前分类的全部子分类

WordPress22.9K阅读模式

在WordPress分类页显示当前分类下的全有子分类链接,可以用下面的代码:

将下面代码添加到当前主题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); 
}
	// 返回根分类的id号
	return $this_category->term_id; 
}

然后在准备显示的位置添加:

<?php wp_list_categories('child_of=' . get_category_root_id($cat) . '&depth=1&hide_empty=0&hierarchical=1&optioncount=1&title_li=');?>

代码源自网络,出处未知。

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
评论  2  访客  1  作者  1
    • web2go
      web2go 1

      请问贵站的代码高亮是什么插件? 本人非常喜欢,请告知!提供下载地址当然最好! 谢谢!

        • 知更鸟
          知更鸟

          @ web2go 这个代码高亮是集成在主题中,找找插件应该有类似的样式

      匿名

      发表评论

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

      拖动滑块以完成验证