WordPress调用分类目录 及输出当前分类下的二级目录 和分类文章数量显示

效果如下:

一级二级分类目录:

%title插图%num

分类计数:

%title插图%num

教程代码

第一步先在主题的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号
}

然后在你要输出的位置放下边的代码即可

调用一级分类(只调用一级分类可以不用在function.php加上边的代码)

<?php
	$parentcat_ID = ($parent_id) ? $parent_id : $cat_ID ;
	$variable = wp_list_categories(array('echo' => false, 'show_count' => false, 'title_li' => '', 'hide_empty' => 0, 'child_of' => $parentcat_ID, 'depth' => 1));
	echo $variable;
?>

调用二级分类

<?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 wp_list_cats('child_of=' . get_category_root_id($cat) . '&depth=1&hide_empty=0&hierarchical=1&optioncount=1');?>

 

 

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 共1条

请登录后发表评论

    • 头像头头博客0