wp_get_post_tags()
云策文档标注
概述
wp_get_post_tags() 是 WordPress 中用于获取文章标签的函数,它基于 wp_get_post_terms() 实现,专门针对 'post_tag' 分类法。该函数返回 WP_Term 对象数组或错误信息,适用于开发者快速检索和处理文章标签。
关键要点
- 函数用于获取指定文章的标签,默认返回所有字段的 WP_Term 对象数组。
- 参数包括可选的 $post_id(默认为 0,不自动使用全局 $post)和 $args 数组(支持 WP_Term_Query 参数)。
- 返回值:成功时返回 WP_Term 对象数组,无标签时返回空数组,若 'post_tag' 分类法不存在则返回 WP_Error 对象。
- 函数内部调用 wp_get_post_terms(),简化了标签获取过程。
- 自 WordPress 2.3.0 版本引入,是处理文章标签的核心函数之一。
代码示例
// 示例1:获取文章标签并打印
$t = wp_get_post_tags($post->ID);
print_r($t);
// 示例2:仅获取标签ID列表
global $post;
$tag_ids = wp_get_post_tags($post->ID, array('fields' => 'ids'));
// 示例3:生成标签链接HTML(注意:示例中未转义,实际使用时需添加转义)
$tags = wp_get_post_tags($post->ID);
$html = '';
foreach ($tags as $tag) {
$tag_link = get_tag_link($tag->term_id);
$html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
$html .= "{$tag->name} ";
}
$html .= '';
echo $html;注意事项
- 使用 $args 参数可以自定义查询,例如设置 'fields' 为 'ids' 以仅获取标签ID。
- 在生成 HTML 输出时,建议对动态内容进行转义以防止安全漏洞。
- 函数不依赖全局 $post 变量,需显式传递 $post_id 参数。
原文内容
Retrieves the tags for a post.
Description
There is only one default for this function, called ‘fields’ and by default is set to ‘all’. There are other defaults that can be overridden in wp_get_object_terms() .
Parameters
$post_idintoptional-
The Post ID. Does not default to the ID of the global $post. Default 0.
$argsarrayoptional-
Tag query parameters.
See WP_Term_Query::__construct() for supported arguments.Default:
array()
Source
function wp_get_post_tags( $post_id = 0, $args = array() ) {
return wp_get_post_terms( $post_id, 'post_tag', $args );
}
Changelog
| Version | Description |
|---|---|
| 2.3.0 | Introduced. |
Skip to note 4 content
Codex
For a post with tags tag2, tag5 and tag6, the code
displays
Array ( [0] => stdClass Object ( [term_id] => 4 [name] => tag2 [slug] => tag2 [term_group] => 0 [term_taxonomy_id] => 4 [taxonomy] => post_tag [description] => [parent] => 0 [count] => 7 ) [1] => stdClass Object ( [term_id] => 7 [name] => tag5 [slug] => tag5 [term_group] => 0 [term_taxonomy_id] => 7 [taxonomy] => post_tag [description] => [parent] => 0 [count] => 6 ) [2] => stdClass Object ( [term_id] => 16 [name] => tag6 [slug] => tag6 [term_group] => 0 [term_taxonomy_id] => 16 [taxonomy] => post_tag [description] => [parent] => 0 [count] => 2 ) )Skip to note 5 content
Codex
To get a list of only the tag IDs for a particular post:
global $post; $tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );and assuming the same dataset as the first example,
$tag_idswould contain[4, 7, 16]Skip to note 6 content
Skye Moroney
Display a post specific list of tags,extending this from an example bhlarsen posted on get_tags() . There is probably a better way to do this.
$tags = wp_get_post_tags($post->ID); //this is the adjustment, all the rest is bhlarsen $html = '<div class="post_tags">'; foreach ( $tags as $tag ) { $tag_link = get_tag_link( $tag->term_id ); $html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>"; $html .= "{$tag->name}</a> "; } $html .= '</div>'; echo $html;