wordpress调用以后分类下子分类的办法

本文实例讲述了wordpress调用以后分类下子分类的办法。分享给大家供大家参考。详细剖析如下:

本人没用过wordpress博客然而集体以为wordpress有函数可间接来子调用以后分类下的子分类的,然而我找了很久没找到,起初找到一具冤家本人的做法,上面我来整顿一下.

在企业网站中,点击根分类时,显示以后根分类下的子分类,这是个很常见的需要,大多cms也能完成这个性能,假如应用wordpress架构,能够吗?

答案是一定的,wordpress也能够完成这样的性能.

其实次要用到wp_list_categorys()函数,该函数的child_of参数是一个数字,显示指定ID(也就是所填的这个数字)下的子分类,这样只需找到以后分类根分类的ID就能够显示了。

the_category_ID()用于显示以后页面的分类ID,默许是输入的,作为参数传递时,最好传入一个false参数,即the_category_ID(false)获取以后分类ID。

接着就是要获取以后分类的父ID,这个也是本文的重中之重,扒了很多材料,也没找到间接能够完成的,不过经过一个函数,倒能够直接获取,代码如下:

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:

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 调用 当前 分类 子分类 方法
版权声明:本文为 安达网络工作室 转载文章,如有侵权请联系我们及时删除。
相关文章
Wordpress将选中内容分享到新浪腾讯微博的办法

本文实例讲述了Wordpress将选中内容分享到新浪腾讯微博的办法。分享给大家供大家参考。详细办法如下: 1、引...

php-cgi过程cpu负载过高:禁用wordpress定时义务wp-cron.php

一、成绩形容 1、19号手动晋级到wordpress4.1,21号早上7点看网站php探针,发现零碎负载过高,差不多在1到1...

WordPress必备数据库SQL查问语句整顿

最近明月给博客和主站都部署了SSL证书,彻彻底底的退出了HTTPS站点行列。这个时期也用到了SQL查问语句来批量...

wordpress评论者链接在新窗口中关上的办法

找到wp-includes/comment-template.php文件中 复制代码代码如下:if ( empty( $url ) || 'http://' == $url ...

详解WordPress中增加和执举动作的函数应用办法

add_action()(增加举措) add_action() 函数用来挂载一个函数到举措钩子上。 用法 add_action( $tag, $fu...

WordPress获取文章内容摘要的办法

本文实例讲述了WordPress获取文章内容摘要的办法。分享给大家供大家参考。详细如下: 第一个函数是讲述了能...

需求提交

客服服务