WordPress:Function Reference/get categories
Description
描述
Returns an array of category objects matching the query parameters.
返回与查询参数匹配的类别objects的数组。
Arguments are pretty much the same as wp_list_categories and can be passed as either array or in query syntax.
参数与wp_list_categories非常相像,可以用来作为数组传递,或者在查询语法中传递。<!—返回的数组似乎总是flat (没有嵌套的子类别)但是我还不确定。-->
Usage
用法
%%% <?php $categories = get_categories(parameters); ?> %%%
%%% <?php $categories = get_categories(parameters); ?> %%%
Examples
Default Usage
例子
默认用法
%%% <?php $defaults = array('type' => 'post', 'child_of' => 0, 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => true, 'include_last_update_time' => false, 'hierarchical' => 1, 'exclude' => , 'include' => , 'number' => , 'pad_counts' => false);?>%%%
%%% <?php $defaults = array('type' => 'post', 'child_of' => 0, 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => true, 'include_last_update_time' => false, 'hierarchical' => 1, 'exclude' => , 'include' => , 'number' => , 'pad_counts' => false);?>%%%
Dropdown Box
下拉列表框
Here's how to create a dropdown box of the subcategories of, say, a category that archives information on past events. This mirrors the example of the dropdown example of wp_get_archives which shows how to create a dropdown box for monthly archives.
下面是创建如,存档有文章记录的类别的子类别的下拉列表框。这反映了 wp_get_archives下面列表的例子, wp_get_archives显示怎样为按月的存档创建下拉列表框。
Suppose the category whose subcategories you want to show is category 10, and that its category "nicename" is "archives".
加入你想要显示的子类别的上级类别是10,而且类别"昵称"是"存档"。
<select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo attribute_escape(__('Select Event')); ?></option> <?php $categories= get_categories('child_of=10'); foreach ($categories as $cat) { $option = '<option value="/category/archives/'.$cat->category_nicename.'">'; $option .= $cat->cat_name; $option .= ' ('.$cat->category_count.')'; $option .= '</option>'; echo $option; } ?> </select>
<select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo attribute_escape(__('Select Event')); ?></option> <?php $categories= get_categories('child_of=10'); foreach ($categories as $cat) { $option = '<option value="/category/archives/'.$cat->category_nicename.'">'; $option .= $cat->cat_name; $option .= ' ('.$cat->category_count.')'; $option .= '</option>'; echo $option; } ?> </select>
Parameters
参数
- type (string)
- Type of category to retrieve
- type (string)
- 需要返回的类别的类型
- post - default
- link
- post - 默认
- link
- child_of (integer)
- Only display categories that are children of the category identified by its ID. There is no default for this parameter. If the parameter is used, the hide_empty parameter is set to false.
- child_of (integer)
- 只显示某个类别ID的子类别。这个参数没有默认设置。如果使用了这个参数,hide_empty参数设置为错误的。
- orderby (string)
- Sort categories alphabetically or by unique category ID. The default is sort by Category ID. Valid values:
- orderby (string)
- 根据字母表属性或者类别ID给类别分类。默认为根据类别 ID分类。有效的参数值:
- ID - default
- name
- ID - 默认
- name
- order (string)
- Sort order for categories (either ascending or descending). The default is ascending. Valid values:
- order (string)
- 类别的分类顺序(升序或者降序)。默认为升序。有效的参数值:
- asc - default
- desc
- asc - 默认
- desc
- hide_empty (boolean)
- Toggles the display of categories with no posts. The default is true (hide empty categories). Valid values:
- hide_empty (boolean)
- Toggles没有文章的类别的显示。默认为正确的(隐藏空类别)。有效的参数值:
- 1 (true) - default
- 0 (false)
- 1 (true) - 默认的
- 0 (false)
- include_last_update_time (boolean)
- Uncertain what this doesies|the example]].
- include_last_update_time (boolean)
- 不确定what this doesies|the example]]。
- 1 (true)
- 0 (false) -- default
- 1 (true)
- 0 (false) -- 默认
- hierarchical (boolean)
- Display sub-categories as inner list items (below the parent list item) or inline. The default is true (display sub-categories below the parent list item). Valid values:
- hierarchical (boolean)
- 将子类别显示为内嵌的列表内容(母类别下面)或者内套。默认为正确的(在母列表内容的下面显示子类别)。有效的参数值:
- 1 (true) - default
- 0 (false)
- 1 (true) -默认的
- 0 (false)
- exclude (string)
- Excludes one or more categories from the list generated by wp_list_categories. This parameter takes a comma-separated list of categories by unique ID, in ascending order.
- exclude (string)
- 从wp_list_categories产生的列表中删除一个或者更多的类别。这个参数有一个用逗号分开的类别ID,以升序排列。
See the example.
请看看例子。
- include (string)
- Only include certain categories in the list generated by wp_list_categories. This parameter takes a comma-separated list of categories by unique ID, in ascending order. See the example.
- include (string)
- 在wp_list_categories产生的列表中只包含某些类别。这个参数有一个用逗号分开的类别ID的列表,以升序排列。
请看看例子。
- list - default.
- none
- number (string)
- The number of categories to return
- pad_counts (boolean)
- Calculates link or post counts by including items from child categories. Valid values:
- 1 (true)
- 0 (false) - default
- list -默认。
- none
- number (string)
- 返回的类别数字
- pad_counts (boolean)
- 包含子类别的内容,技术链接或者文章字数。有效的参数值:
- 1 (true)
- 0 (false) -默认