get_category()
get_category( int|object $category, string $output = OB…
get_category( int|object $category, string $output = OBJECT, string $filter = ‘raw’ )
根据类别ID或类别对象检索类别数据。
Retrieves category data given a category ID or category object.
根据类别ID或类别对象检索类别数据。
Retrieves category data given a category ID or category object.
目录锚点:#说明#参数#返回#源码#笔记
说明(Description)
如果向$category参数传递一个对象,则假定该对象是检索到数据库的category行对象。它将缓存类别数据。
如果向$category传递一个category ID的整数,则将从数据库中检索该类别(如果尚未缓存),并将其传回。
如果查看get_term(),那么这两种类型都将通过几个过滤器,最后根据$filter参数值进行清理。
参数(Parameters)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| $category | (int | object) | 必需 | 类别ID或类别行对象 |
| $output | (string) | 可选 | 所需的返回类型。数组或数组中的一个,分别对应于WP术语对象、关联数组或数值数组。 |
| $filter | (string) | 可选 | 默认为raw或不应用WordPress定义的过滤器。 |
返回(Return)
$output参数定义的类型中的(object|array|WP|Error|null)类别数据。WP_Error如果$category为空,如果不存在则为空。
源码(Source)
/**
* Retrieves category data given a category ID or category object.
*
* If you pass the $category parameter an object, which is assumed to be the
* category row object retrieved the database. It will cache the category data.
*
* If you pass $category an integer of the category ID, then that category will
* be retrieved from the database, if it isn't already cached, and pass it back.
*
* If you look at get_term(), then both types will be passed through several
* filters and finally sanitized based on the $filter parameter value.
*
* The category will converted to maintain backwards compatibility.
*
* @since 1.5.1
*
* @param int|object $category Category ID or Category row object
* @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
* @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
* @return object|array|WP_Error|null Category data in type defined by $output parameter.
* WP_Error if $category is empty, null if it does not exist.
*/
function get_category( $category, $output = OBJECT, $filter = 'raw' ) {
$category = get_term( $category, 'category', $output, $filter );
if ( is_wp_error( $category ) )
return $category;
_make_cat_compat( $category );
return $category;
}| 更新版本 | 源码位置 | 使用 | 被使用 |
|---|---|---|---|
| 1.5.1 | wp-includes/category.php:89 | 2 | 3 |
笔记(Notes)
传递类别ID(整数)的示例:
检查具有cat ID的类别是否有任何数量的帖子。
获取类别模板上的当前类别数据:
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

还没有任何评论,赶紧来占个楼吧!