合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# 输入当前顶级分类的子分类 > 使用wordpress建网站时,在侧边栏往往需要调用某个当前顶级分类下的子分类,这样只要在网站后台添加分类后,网站界面就会自动的调用显示出来。 > 本内容来自网络分享,以下为原文链接: > @link [https://www.xuewangzhan.net/wpbbs/6770.html](https://www.xuewangzhan.net/wpbbs/6770.html) ![](https://img.kancloud.cn/ab/f9/abf9ff0db50cd1bf01ec82b2d00a8974_418x98.png) ## 在function.php中加入如下代码: ``` /** * 获取当前栏目的顶级栏目。 * 获取当前顶级分类下的子分类。 * * @link https://blog.csdn.net/weixin_33985507/article/details/91900600 * @link https://www.seo628.com/2547.html * * 用法实例 * <?php 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>'; } ?> * */ 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号 } ``` ## 实际运用举例(在边栏sidabar.php中加入如下代码) ``` <?php 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>'; } ?> ```