函数文档

get_category()

💡 云策文档标注

概述

get_category() 函数用于根据分类 ID 或分类对象检索分类数据,返回 WP_Term 对象或数组,并支持缓存和过滤。

关键要点

  • 参数 $category 可以是整数(分类 ID)或对象(分类行对象),函数会从数据库检索或使用缓存数据。
  • 参数 $output 指定返回类型:OBJECT(默认,WP_Term 对象)、ARRAY_A(关联数组)或 ARRAY_N(数字数组)。
  • 参数 $filter 控制字段的清理方式,默认值为 'raw'。
  • 返回值为 WP_Term 对象、数组、WP_Error 或 null,具体取决于 $output 参数和分类是否存在。
  • 函数内部调用 get_term() 并应用 _make_cat_compat() 以保持向后兼容性。

代码示例

// 示例:传递分类 ID(整数)并打印结果
$category = get_category(106);
print_r($category);
// 输出类似:stdClass Object ( [term_id] => 106 [name] => Category Name ... )
// 示例:检查分类是否有多个帖子
if ( get_category($id)->category_count > 1 ) {
   // 执行操作
}
// 示例:在分类模板中获取当前分类数据
$current_category = get_category( get_query_var('cat'), false );

注意事项

  • 如果 $category 为空,函数返回 WP_Error;如果分类不存在,返回 null。
  • 返回的 WP_Term 对象包含向后兼容的属性别名(如 cat_ID、category_count)。
  • 函数依赖于 get_term() 和 _make_cat_compat(),确保与旧版本兼容。

📄 原文内容

Retrieves category data given a category ID or category object.

Description

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.

Parameters

$categoryint|objectrequired
Category ID or category row object.
$outputstringoptional
The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Term object, an associative array, or a numeric array, respectively.

Default:OBJECT

$filterstringoptional
How to sanitize category fields. Default 'raw'.

Return

WP_Term|array|WP_Error|null Category data in type defined by $output parameter.
Returns a WP_Term object with backwards compatible property aliases filled in.
WP_Error if $category is empty, null if it does not exist.

Source

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;
}

Changelog

Version Description
1.5.1 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    Example passing Category ID (integer):

    The output of print_r( $category ); would be similar to:
    <br />
    stdClass Object<br />
    (<br />
    [term_id] => 106<br />
    [name] => Category Name<br />
    [slug] => category-name<br />
    [term_group] => 0<br />
    [term_taxonomy_id] => 108<br />
    [taxonomy] => category<br />
    [description] => This is the category description.<br />
    [parent] => 62<br />
    [count] => 17<br />
    [filter] => raw<br />
    [cat_ID] => 106<br />
    [category_count] => 17<br />
    [category_description] => This is the category description.<br />
    [cat_name] => Category Name<br />
    [category_nicename] => category-name<br />
    [category_parent] => 62<br />
    )<br />