get_tag()
云策文档标注
概述
get_tag() 函数用于通过标签 ID 或标签对象检索文章标签。它基于 get_term() 实现,支持多种返回类型和过滤选项。
关键要点
- 参数 $tag 可以是整数(标签 ID)或对象(如数据库中的标签行对象),用于指定要检索的标签。
- 参数 $output 控制返回类型,可选 OBJECT(WP_Term 对象)、ARRAY_A(关联数组)或 ARRAY_N(数字数组),默认值为 OBJECT。
- 参数 $filter 定义如何清理标签字段,默认值为 'raw'。
- 返回值为 WP_Term 对象、数组、WP_Error 或 null,具体取决于 $output 参数和标签是否存在。
- 函数内部调用 get_term(),传递 'post_tag' 作为分类法参数,确保数据经过过滤和清理。
代码示例
function get_tag( $tag, $output = OBJECT, $filter = 'raw' ) {
return get_term( $tag, 'post_tag', $output, $filter );
}
原文内容
Retrieves a post tag by tag ID or tag object.
Description
If you pass the $tag parameter an object, which is assumed to be the tag row object retrieved from the database, it will cache the tag data.
If you pass $tag an integer of the tag ID, then that tag will be retrieved from the database, if it isn’t already cached, and passed back.
If you look at get_term() , both types will be passed through several filters and finally sanitized based on the $filter parameter value.
Parameters
$tagint|WP_Term|objectrequired-
A tag ID or 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 tag fields. Default
'raw'.
Source
function get_tag( $tag, $output = OBJECT, $filter = 'raw' ) {
return get_term( $tag, 'post_tag', $output, $filter );
}
Changelog
| Version | Description |
|---|---|
| 2.3.0 | Introduced. |