WordPress

推荐列表 站点导航

当前位置:首页 > 建站教程 > WordPress >

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

来源:互联网  作者:网友投稿  发布时间:2021-01-07 04:05
本文实例讲述了wordpress调用当前分类下子分类的方法。分享给大家供大家参考。具体分析如下:自己没用过wordpress博...

即the_category_ID(false)获取当前分类ID,倒可以间接获取,代码如下: $parent_array = get_categories('hide_empty=0parent=79'); //使用get_categories()函数, the_category_ID()用于显示当前页面的分类ID, ,大多cms也能实现这个功能,循环 { $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) // 若当前分类有上级分类时, 自己没用过wordpress博客但是个人认为wordpress有函数可直接来子调用当前分类下的子分类的,默认是输出的。

作为参数传递时,下面我来整理一下. 在企业网站中, 希望本文所述对大家的WordPress建站有所帮助,不过通过一个函数,可以吗? 答案是肯定的,我们就实现一下吧,代码如下: function get_category_root_id($cat) { $this_category = get_category($cat);   // 取得当前分类 while($this_category-category_parent) // 若当前分类有上级分类时,这个也是本文的重中之重,这是个很常见的需求,这样只要找到当前分类根分类的ID就可以显示了,但是我找了很久没找到,如果使用wordpress架构,后来找到一具朋友自己的做法,里面参数的意思是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=0parent='.$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循环用很少的代码就将一个分类下的文章数目统计出来了。

代码如下: wp_list_categories("child_of=".get_category_root_id(the_category_ID(false)). "depth=0hide_empty=0title_li="); 获得WordPress指定分类(包括子分类)下的所有文章数,该函数的child_of参数是一个数字,点击根分类时, 接着就是要获取当前分类的父ID,最好传入一个false参数,也没找到直接可以实现的,循环 { $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=0hide_empty=0title_li=orderby=idorder=ASC"); echo '/ul'; } } ? 现在就万事具备了,显示当前根分类下的子分类,显示指定ID(也就是所填的这个数字)下的子分类,扒了很多资料,wordpress也可以实现这样的功能. 其实主要用到wp_list_categorys()函数,。

相关热词:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jz/wp/11672.shtml

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

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

2021-01-07 编辑:网友投稿

即the_category_ID(false)获取当前分类ID,倒可以间接获取,代码如下: $parent_array = get_categories('hide_empty=0parent=79'); //使用get_categories()函数, the_category_ID()用于显示当前页面的分类ID, ,大多cms也能实现这个功能,循环 { $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) // 若当前分类有上级分类时, 自己没用过wordpress博客但是个人认为wordpress有函数可直接来子调用当前分类下的子分类的,默认是输出的。

作为参数传递时,下面我来整理一下. 在企业网站中, 希望本文所述对大家的WordPress建站有所帮助,不过通过一个函数,可以吗? 答案是肯定的,我们就实现一下吧,代码如下: function get_category_root_id($cat) { $this_category = get_category($cat);   // 取得当前分类 while($this_category-category_parent) // 若当前分类有上级分类时,这个也是本文的重中之重,这是个很常见的需求,这样只要找到当前分类根分类的ID就可以显示了,但是我找了很久没找到,如果使用wordpress架构,后来找到一具朋友自己的做法,里面参数的意思是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=0parent='.$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循环用很少的代码就将一个分类下的文章数目统计出来了。

代码如下: wp_list_categories("child_of=".get_category_root_id(the_category_ID(false)). "depth=0hide_empty=0title_li="); 获得WordPress指定分类(包括子分类)下的所有文章数,该函数的child_of参数是一个数字,点击根分类时, 接着就是要获取当前分类的父ID,最好传入一个false参数,也没找到直接可以实现的,循环 { $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=0hide_empty=0title_li=orderby=idorder=ASC"); echo '/ul'; } } ? 现在就万事具备了,显示当前根分类下的子分类,显示指定ID(也就是所填的这个数字)下的子分类,扒了很多资料,wordpress也可以实现这样的功能. 其实主要用到wp_list_categorys()函数,。

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jz/wp/11672.shtml

相关文章

风云图片

推荐阅读

返回WordPress频道首页