wordpress获取当前分类的子分类
2024-09-29 17:20:07
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 显示分类
); ?>
最新文章
- mysql中boolean类型
- Delphi dll 断点调试
- Linux 查找文件并删除文件内容
- BZOJ 3932 [CQOI2015]任务查询系统 ——可持久化线段树
- tab模块
- 第二百二十三天 how can I 坚持
- QT中窗口刷新事件的学习总结
- Git打补丁常见问题
- MAC图片格式转换
- 编写高质量代码:改善Java程序的151个建议(第一章:JAVA开发中通用的方法和准则)
- windows 10 防火墙设置规则:允许特定ip端口
- js上传图片压缩,并转化为base64
- my live boadband
- vue里的样式添加之行间样式
- 第 8 章 容器网络 - 071 - 如何定制 Calico 的 IP 池?
- logstash收集IIS日志
- Apache Hadoop YARN: 背景及概述
- Oracle EBS SLA 详解(转)
- 深入解读《Gartner2017年商业智能和分析平台魔力象限报告》
- E. The Supersonic Rocket Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2)