wordpress调用当前分类下子分类的方法


当前第2页 返回上一页

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>';
}
}
?>
现在就万事具备了,我们就实现一下吧,代码如下:

代码如下:
wp_list_categories("child_of=".get_category_root_id(the_category_ID(false)). "&depth=0&hide_empty=0&title_li=");
获得WordPress指定分类(包括子分类)下的所有文章数,代码如下:

代码如下:
$parent_array = get_categories('hide_empty=0&parent=79');
//使用get_categories()函数,里面参数的意思是hide_empty把子分类下没有文章的也显示出来
//parent 父级分类的ID号
foreach($parent_array as $k=>$v) //第一步
{
$sub_parent_array = get_categories('parent='.$v->cat_ID);
foreach($sub_parent_array as $kk=>$vv) //第二步
{
$three_parent_array = get_categories('hide_empty=0&parent='.$vv->cat_ID);
foreach($three_parent_array as $kkk=>$vvv) //第三步
{
$three_count +=$vvv->category_count; //第三极子分类下文章数进行统计
}
$sub_count +=$vv->category_count; //第二级子分类下文章数进行统计
}
$count +=$v->category_count; //第一级子分类下文章数进行统计
}
$total = $count+$sub_count+$three_count;
//将第一级和第二级和第三级统计的文章数目进行相加后放到一个变量中。

这样我们通过php的foreach循环用很少的代码就将一个分类下的文章数目统计出来了。

希望本文所述对大家的WordPress建站有所帮助。


标签:WordPress

返回前面的内容

相关阅读 >>

wordpress 主流的blog平台

wordpress迁移后图片地址错误问题的解决方法

wordpress中自定义后台管理界面配色方案的小技巧

wordpress主题制作中自定义头部的相关php函数解析

nginx服务lnmp之wordpress部署流程步骤

详解wordpress开发中的get_post与get_posts函数使用

wordpress如何安装,wordpress安装过程

wordpress修改评论默认头像的方法

wordpress主题制作涉及到的基本模板及说明整理

wordpress设置友情链接只在首页显示的方法

更多相关阅读请进入《wordpress》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...