本文实例讲述了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 的小工具(widget)是一大特征,它让用户自在拖动组合内容,而且任何插件和主题...
找到wp-includes/formatting.php文件中复制代码代码如下:// This is not a tag, nor is the texturization ...
从ThemeForest购买的是正版商业主题AVADA(有售后技术支持),应用国际阿里云主机(配置不算低),网站大局...
查问字符串指的是链接中后边的问号后的查问语句,格局为 key=value,多个查问语句用 & 符号离开。add_query...
本文实例讲述了WordPress评论中制止HTML代码显示的办法。分享给大家供大家参考。详细剖析如下: 应用WordPr...
比方本人创立了一个主题,那么需求在后盾增加一些设置选项,所以就触及到了后盾增加顶级菜单的需要: 复制代...