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 ); ?>
默认用法输出的效果:
<?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 显示分类
); ?>
原文:https://www.cnblogs.com/xiaobingch/p/10458196.html