WordPress: Function Reference/get categories:修订间差异
(新页面: == Description == Returns an array of category objects matching the query parameters. Arguments are pretty much the same as [[WordPress:Template_Tags/wp_list_categories|wp_list_categori...) |
无编辑摘要 |
||
第1行: | 第1行: | ||
== Description == | == Description == | ||
==描述 == | |||
Returns an array of category objects matching the query parameters. | Returns an array of category objects matching the query parameters. | ||
返回与查询参数匹配的类别objects的数组。 | |||
Arguments are pretty much the same as [[WordPress:Template_Tags/wp_list_categories|wp_list_categories]] and can be passed as either array or in query syntax. <!--The array returned seems to be always flat (no nesting for subcategories) but I'm not sure yet.--> | Arguments are pretty much the same as [[WordPress:Template_Tags/wp_list_categories|wp_list_categories]] and can be passed as either array or in query syntax. <!--The array returned seems to be always flat (no nesting for subcategories) but I'm not sure yet.--> | ||
参数与[[WordPress:Template_Tags/wp_list_categories|wp_list_categories]]非常相像,可以用来作为数组传递,或者在查询语法中传递。<!—返回的数组似乎总是flat (没有嵌套的子类别)但是我还不确定。--> | |||
== Usage == | == Usage == | ||
== 用法 == | |||
%%% <?php $categories = get_categories(parameters); ?> %%% | |||
%%% <?php $categories = get_categories(parameters); ?> %%% | %%% <?php $categories = get_categories(parameters); ?> %%% | ||
第12行: | 第22行: | ||
=== Default Usage === | === 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', | %%% <?php $defaults = array('type' => 'post', | ||
'child_of' => 0, | 'child_of' => 0, | ||
第25行: | 第52行: | ||
=== Dropdown Box === | === 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 [[WordPress:Template_Tags/wp_get_archives | wp_get_archives]] which shows how to create a dropdown box for monthly archives. | 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 [[WordPress:Template_Tags/wp_get_archives | wp_get_archives]] which shows how to create a dropdown box for monthly archives. | ||
下面是创建如,存档有文章记录的类别的子类别的下拉列表框。这反映了[[WordPress:Template_Tags/wp_get_archives | wp_get_archives]]下面列表的例子,[[WordPress:Template_Tags/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". | Suppose the category whose subcategories you want to show is category 10, and that its category "nicename" is "archives". | ||
加入你想要显示的子类别的上级类别是10,而且类别"昵称"是"存档"。 | |||
<pre> | |||
<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> | |||
</pre> | |||
<pre> | <pre> | ||
<select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> | <select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> | ||
第43行: | 第99行: | ||
</select> | </select> | ||
</pre> | </pre> | ||
== Parameters == | == Parameters == | ||
; '''type''' (''string'') : Type of category to | |||
== 参数 == | |||
; '''type''' (''string'') : Type of category to retrieve | |||
; '''type''' (''string'') :需要返回的类别的类型 | |||
:* <tt>post</tt> - default | :* <tt>post</tt> - default | ||
:* <tt>link</tt> | |||
:* <tt>post</tt> - 默认 | |||
:* <tt>link</tt> | :* <tt>link</tt> | ||
; '''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'') :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''):Sort categories alphabetically or by unique category ID. The default is ''sort by Category ID''. Valid values: | ||
; '''orderby''' (''string''):根据字母表属性或者类别ID给类别分类。默认为''根据类别 ID分类''。有效的参数值: | |||
:* <tt>ID</tt> - default | :* <tt>ID</tt> - default | ||
:* <tt>name</tt> | |||
:* <tt>ID</tt> - 默认 | |||
:* <tt>name</tt> | :* <tt>name</tt> | ||
; '''order''' (''string''):Sort order for categories (either ascending or descending). The default is ''ascending''. Valid values: | ; '''order''' (''string''):Sort order for categories (either ascending or descending). The default is ''ascending''. Valid values: | ||
; '''order''' (''string''):类别的分类顺序(升序或者降序)。默认为''升序''。有效的参数值: | |||
:* <tt>asc</tt> - default | :* <tt>asc</tt> - default | ||
:* <tt>desc</tt> | |||
:* <tt>asc</tt> - 默认 | |||
:* <tt>desc</tt> | :* <tt>desc</tt> | ||
; '''hide_empty''' (''boolean''):Toggles the display of categories with no posts. The default is ''true'' (hide empty categories). Valid values: | ; '''hide_empty''' (''boolean''):Toggles the display of categories with no posts. The default is ''true'' (hide empty categories). Valid values: | ||
; '''hide_empty''' (''boolean''): Toggles没有文章的类别的显示。默认为''正确的''(隐藏空类别)。有效的参数值: | |||
:* <tt>1 (true)</tt> - default | :* <tt>1 (true)</tt> - default | ||
:* <tt>0 (false)</tt> | |||
:* <tt>1 (true)</tt> - 默认的 | |||
:* <tt>0 (false)</tt> | :* <tt>0 (false)</tt> | ||
; '''include_last_update_time''' (''boolean''): Uncertain what this doesies|the example]]. | ; '''include_last_update_time''' (''boolean''): Uncertain what this doesies|the example]]. | ||
; '''include_last_update_time''' (''boolean''):不确定what this doesies|the example]]。 | |||
:* <tt>1 (true)</tt> | :* <tt>1 (true)</tt> | ||
:* <tt>0 (false)</tt> -- default | :* <tt>0 (false)</tt> -- default | ||
:* <tt>1 (true)</tt> | |||
:* <tt>0 (false)</tt> -- 默认 | |||
; '''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''):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''):将子类别显示为内嵌的列表内容(母类别下面)或者内套。默认为''正确的''(在母列表内容的下面显示子类别)。有效的参数值: | |||
:* <tt>1 (true)</tt> - default | :* <tt>1 (true)</tt> - default | ||
:* <tt>0 (false)</tt> | :* <tt>0 (false)</tt> | ||
; '''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. See [[WordPress:Template_Tags/wp_list_categories#Include_or_Exclude_Categories|the example]]. | :* <tt>1 (true)</tt> -默认的 | ||
:* <tt>0 (false)</tt> | |||
; '''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 [[WordPress:Template_Tags/wp_list_categories#Include_or_Exclude_Categories|the example]]. | |||
请看看[[WordPress:Template_Tags/wp_list_categories#Include_or_Exclude_Categories|例子]]。 | |||
; '''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 [[WordPress:Template_Tags/wp_list_categories#Include_or_Exclude_Categories|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 [[WordPress:Template_Tags/wp_list_categories#Include_or_Exclude_Categories|the example]]. | ||
; '''include''' (''string''):在''wp_list_categories''产生的列表中只包含某些类别。这个参数有一个用逗号分开的类别ID的列表,以升序排列。 | |||
请看看[[WordPress:Template_Tags/wp_list_categories#Include_or_Exclude_Categories|例子]]。 | |||
:* <tt>list</tt> - default. | :* <tt>list</tt> - default. | ||
:* <tt>none</tt> | :* <tt>none</tt> | ||
第72行: | 第178行: | ||
:* <tt>1 (true)</tt> | :* <tt>1 (true)</tt> | ||
:* <tt>0 (false)</tt> - default | :* <tt>0 (false)</tt> - default | ||
:* <tt>list</tt> -默认。 | |||
:* <tt>none</tt> | |||
; '''number''' (''string'') : 返回的类别数字 | |||
; '''pad_counts''' (''boolean''): 包含子类别的内容,技术链接或者文章字数。有效的参数值: | |||
:* <tt>1 (true)</tt> | |||
:* <tt>0 (false)</tt> -默认 |
2008年7月19日 (六) 16:10的版本
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) -默认