WordPress函数get_categories()获取所有的分类信息

描述:

获取所有的分类信息

用法:

<?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

(string) 要检索的类别类型

  • post – 默认
  • link

注意:从WordPress3.0开始,type=link已经被弃用。改用taxonomy=link_category。

child_of

(integer) 显示由其ID标识的类别的所有子类别(即子分类和孙分类)。此参数没有默认值。如果使用该参数,则hide_empty参数设置为false。

parent

(integer) 仅显示由其ID标识的分类的直接子代(即仅子代)的类别。这与“child_of”参数不同。此参数没有默认值。[在2.8.4中]

orderby

(string) 按字母顺序或按唯一类别ID对分类排序。默认为按名称排序。有效值:

  • id
  • name – 默认
  • slug
  • count
  • term_group

order

(string) 分类的排序顺序(升序或降序)。默认值为升序。有效值:

  • asc – 默认
  • desc

hide_empty

(boolean) 切换不显示文章的类别的显示。默认值为1表示真,或者可以添加“0”表示假(显示空类别)。有效值:

  • 1 – 默认
  • 0

hierarchical

(boolean) 如果为true,则结果将包括空的子类别,只要这些子类别具有非空的子类别。默认值为true。有效值:

  • 1 (true) – 默认
  • 0 (false)

exclude

(string) 从wp_list_categories生成的列表中排除一个或多个类别。此参数采用按唯一ID以逗号分隔的类别列表(按升序排列)。请参见示例。

include

(string) 只包括wp_list_categories生成的列表中的某些类别。此参数采用按唯一ID以逗号分隔的类别列表(按升序排列)。请参见示例。

  • list – 默认.
  • none

number

(string) 要返回的分类数

taxonomy

(string or array) 要返回的分类法。此参数在版本3.0中添加了有效值:

  • category – 默认
  • taxonomy – 或 任何已注册的分类法

pad_counts

(boolean) 通过包含子分类中的项目来计算链接或投递计数。有效值:

  • 1 (true)
  • 0 (false) – 默认

示例:

<select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> 
 <option value=""><?php echo esc_attr(__('Select Event')); ?></option> 
 <?php 
  $categories = get_categories('child_of=10'); 
  foreach ($categories as $category) {
  	$option = '<option value="/category/archives/'.$category->category_nicename.'">';
	$option .= $category->cat_name;
	$option .= ' ('.$category->category_count.')';
	$option .= '</option>';
	echo $option;
  }
 ?>
</select>

源文件:

function get_categories( $args = '' ) {
    $defaults = array( 'taxonomy' => 'category' );
    $args     = wp_parse_args( $args, $defaults );
 
    /**
     * Filters the taxonomy used to retrieve terms when calling get_categories().
     *
     * @since 2.7.0
     *
     * @param string $taxonomy Taxonomy to retrieve terms from.
     * @param array  $args     An array of arguments. See get_terms().
     */
    $args['taxonomy'] = apply_filters( 'get_categories_taxonomy', $args['taxonomy'], $args );
 
    // Back compat
    if ( isset( $args['type'] ) && 'link' == $args['type'] ) {
        _deprecated_argument(
            __FUNCTION__,
            '3.0.0',
            sprintf(
                /* translators: 1: "type => link", 2: "taxonomy => link_category" */
                __( '%1$s is deprecated. Use %2$s instead.' ),
                '<code>type => link</code>',
                '<code>taxonomy => link_category</code>'
            )
        );
        $args['taxonomy'] = 'link_category';
    }
 
    $categories = get_terms( $args );
 
    if ( is_wp_error( $categories ) ) {
        $categories = array();
    } else {
        $categories = (array) $categories;
        foreach ( array_keys( $categories ) as $k ) {
            _make_cat_compat( $categories[ $k ] );
        }
    }
 
    return $categories;
}