🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 灵活调用指定分类下的子分类 > 使用wordpress建网站时,在侧边栏往往需要调用某个指定分类下的子分类,这样只要在网站后台添加分类后,网站界面就会自动的调用显示出来。 > 本内容来自网络分享,以下为原文链接: > @link [https://www.xuewangzhan.net/wpbbs/6770.html](https://www.xuewangzhan.net/wpbbs/6770.html) > @link [https://www.boke8.net/wordpress-get\_categories.html](https://www.boke8.net/wordpress-get_categories.html) :-: ![](https://img.kancloud.cn/07/6c/076c583e883082944bc3963124cec8f8_245x321.png) ``` <ul> <?php $args=array( 'child_of'=> '4', 'hide_empty'=>'0', 'orderby' => 'name', 'order' => 'ASC', ); $categories=get_categories($args); foreach($categories as $category) { echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "%s" ), $category->name ) . '" ' . '>' . $category->name.'</a></li>'; } ?> </ul> ``` ## **$args参数及默认值:** ``` <?php $args = array( 'type' => 'post', 'child_of' => 0, 'parent' => '', 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 1, 'hierarchical' => 1, 'exclude' => '', 'include' => '', 'number' => '', 'taxonomy' => 'category', 'pad_counts' => false ); ?> ``` ## **参数说明:** **type** (字符)post和link 其中link在新版3.0以后已被弃用。 **child\_of** (整数)仅显示标注了编号的分类的子类。该参数无默认值。使用该参数时应将hide\_empty参数设为false **parent** (整数)只显示某个父级分类以及下面的子分类(注:子分类只显示一个层级)。 **orderby** (字符)将分类按字母顺序或独有分类编号进行排序。默认为按分类 编号排序包括ID(默认)和Name **order** (字符)为类别排序(升序或降序)。默认升序。可能的值包括asc(默认)和desc **hide\_empty** (布尔值)触发显示没有文章的分类。默认值为true(隐藏空类别)。有效的值包括:1(true)和0(false) **hierarchical** (布尔值)将子类作为内部列表项目(父列表项下)的层级关系。默认为true(显示父列表项下的子类)。有效值包括1 (true)和0(false) **exclude** (字符)除去分类列表中一个或多个分类,多个可以用逗号分开,用分类ID号表示 **include** (字符)只包含指定分类ID编号的分类。多个可以用逗号分开,用分类ID号表示 **number** (字符)将要返回的类别数量 **pad\_counts** (布尔值)通过子类中的项来计算链接或文章。有效值包括1(true)和0(false),0为默认 **taxonomy** (字符)返回一个分类法,这个是wordpress3.0版本后新添加的一个参数。返回的值包括category(默认)和taxonomy(一些新定义的分类名称) ## **示例:显示分类列表和分类描述以及包含的文章数目** ``` <?php $args=array( 'orderby' => 'name', 'order' => 'ASC' ); $categories=get_categories($args); foreach($categories as $category) { echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> '; echo '<p> Description:'. $category->description . '</p>'; echo '<p> Post Count: '. $category->count . '</p>'; } ?> ```