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 显示分类
); ?>