本文实例讲述了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号
}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号
}<?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=");$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搬家,置信大局部wp玩家还是可以轻松处理成绩,可是关于老手置信会遇到不少费事成绩吧,上面跟...
WordPress在后盾编辑日志时编辑框左下角有一个字数统计,不过只显示在后盾,能不能在前台也加上文章字数统计...
比方: A 留言了, B 用 @ 回复了 A, 所以 B 的回复可能是这样的: @A How much money do you have? 就是说...
WordPress在线装置主题、插件,或在应用疾速公布的文章中含有图片时,会提醒: 正在装置主题:Frontier 1.0...
WordPress应用MySQL数据库。作为一个开发者,咱们有必要把握WordPress数据库的根本结构,并在本人的插件或主...
然而将站点部署到一个Windows XP 中文版上时,发现上传的附件在效劳器的文件名为乱码,而URL是失常的,阐明...