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.然后在页面要显示二级分类的地方粘贴下面这段代码即
<?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>'; } } ?>
<?php $args = array( 'show_option_all' => '', 'orderby' => 'name', 'order' => 'ASC', 'style' => 'list', 'show_count' => 0, 'hide_empty' => 1, 'use_desc_for_title' => 1, 'child_of' => 0, 'feed' => '', 'feed_type' => '', 'feed_image' => '', 'exclude' => '', 'exclude_tree' => '', 'include' => '', 'hierarchical' => 1, 'title_li' => __( 'Categories' ), 'show_option_none' => __('No categories'), 'number' => null, 'echo' => 1, 'depth' => 0, 'current_category' => 0, 'pad_counts' => 0, 'taxonomy' => 'category', 'walker' => null ); ?>
默认用法输出的效果:
- 无连接的分类
- 根据分类名称对分类列表进行升序排列
- 以无序列表的样式显示
- 不显示文章数量
- 只显示有文章的分类
- 设置标题属性到分类描述
- 子分类无限制
- 不显示Feed和Feed图像
- 不排除任何分类,并包括所有分类
- 为当前的分类添加CSS类’current-cat’
- 以分层缩进的方式显示分类列表
- 在列表的顶部显示“分类(Categories)”作为标题
- 没有SQL限制(’number’ => 0 is not shown above)
- 显示(输出)分类
- 不限制显示的深度
- 所有分类
- 使用一个新的Walker_Category 类对象 walker 来显示列表
<?php $args = array(
‘show_option_all’ => “, 无链接的分类 ‘orderby’ => ‘name’, 按照分类名排序 ‘order’=> ‘ASC’, 升序 ‘show_last_update’ => 0, 不显示分类中日志的最新时间戳 ‘style’ => ‘list’, 用列表显示分类 ‘show_count’ => 0, 0, 不显示分类下的日志数 ‘hide_empty’ => 1, Displays only Categories with posts ‘use_desc_for_title’ => 1, 显示分类链接中 title 标签的分类描述 ‘child_of’ => 0, 子分类无限制 ‘feed’ => ”, 无 feed ‘feed_image’ => ”, 无 feed 图片显示 ‘exclude’ => ”, 不在分类列表中显示该分类 ‘hierarchical’ => true, 分层显示父/子分类 ‘title_li’ => __(‘Categories’), 在列表前作为标题显示分类 ‘echo’ => 1 显示分类
); ?>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)