get_the_tags()
云策文档标注
概述
get_the_tags() 函数用于检索文章关联的标签,返回一个 WP_Term 对象数组。该函数不直接输出内容,开发者需手动处理返回的数据以显示或使用标签信息。
关键要点
- 参数 $post 为可选,可传入文章 ID 或 WP_Post 对象;在 The Loop 中可省略参数。
- 返回值类型为 WP_Term[]|false|WP_Error:成功时返回 WP_Term 对象数组,无标签或文章不存在时返回 false,失败时返回 WP_Error。
- 函数内部调用 get_the_terms() 并应用 'get_the_tags' 过滤器,允许自定义标签数组。
- 适用于多种场景,如显示标签名称、链接到标签页面、创建下拉菜单或基于标签值执行条件代码。
代码示例
// 示例:显示当前文章的所有标签名称(用逗号分隔)
$post_tags = get_the_tags();
if ( $post_tags ) {
foreach( $post_tags as $tag ) {
echo $tag->name . ', ';
}
}注意事项
- 函数本身不输出任何内容,需通过 echo 或循环访问对象属性来显示标签。
- 在 The Loop 外使用时,建议显式传递文章 ID,如 get_the_tags( get_the_ID() )。
- 返回的数组可能为空,使用前应检查 $post_tags 是否为真以避免错误。
原文内容
Retrieves the tags for a post.
Parameters
$postint|WP_Postrequired-
Post ID or object.
Source
function get_the_tags( $post = 0 ) {
$terms = get_the_terms( $post, 'post_tag' );
/**
* Filters the array of tags for the given post.
*
* @since 2.3.0
*
* @see get_the_terms()
*
* @param WP_Term[]|false|WP_Error $terms Array of WP_Term objects on success, false if there are no terms
* or the post does not exist, WP_Error on failure.
*/
return apply_filters( 'get_the_tags', $terms );
}
Hooks
- apply_filters( ‘get_the_tags’, WP_Term[]|false|WP_Error $terms )
-
Filters the array of tags for the given post.
Changelog
| Version | Description |
|---|---|
| 2.3.0 | Introduced. |
Skip to note 9 content
hearvox
Print only the first tag name:
$post_tags = get_the_tags(); if ( $post_tags ) { echo $post_tags[0]->name; }Skip to note 10 content
hearvox
This example prints the tags of current post:
Code must be used in The Loop.
$post_tags = get_the_tags(); if ( $post_tags ) { foreach( $post_tags as $tag ) { echo $tag->name . ', '; } }$post_tags = get_the_tags( get_the_ID() ); if ( $post_tags ) { foreach( $post_tags as $tag ) { echo __( $tag->name . ', ' ); } }Skip to note 11 content
marzian
// Show post tags with link and a custom separator
function wpdocs_show_tags() { $post_tags = get_the_tags(); $separator = ' | '; $output = ''; if ( ! empty( $post_tags ) ) { foreach ( $post_tags as $tag ) { $output .= '<a href="' . esc_attr( get_tag_link( $tag->term_id ) ) . '">' . __( $tag->name ) . '</a>' . $separator; } } return trim( $output, $separator ); }Skip to note 12 content
hearvox
Example using post ID to get tags:
This doesn’t need to be in The Loop.
$post_tags = get_the_tags( 24 ); print_r( $post_tags ); /* This above prints the tag objects for post ID #24 (if post has any tags): Array ( [0] => WP_Term Object ( [term_id] => 108 [name] => tag-1 [slug] => tag-1 [term_group] => 0 [term_taxonomy_id] => 109 [taxonomy] => post_tag [description] => [parent] => 0 [count] => 1 [filter] => raw [object_id] => 24 ) [1] => WP_Term Object ( [term_id] => 109 [name] => tag-2 [slug] => tag-2 [term_group] => 0 [term_taxonomy_id] => 110 [taxonomy] => post_tag [description] => [parent] => 0 [count] => 1 [filter] => raw [object_id] => 24 ) ) */Skip to note 13 content
exonfang
Display tags with links to tag pages in an unordered list:
$post_tags = get_the_tags(); if ( ! empty( $post_tags ) ) { echo '<ul>'; foreach( $post_tags as $post_tag ) { echo '<li><a href="' . get_tag_link( $post_tag ) . '">' . $post_tag->name . '</a></li>'; } echo '</ul>'; }Skip to note 14 content
hearvox
Execute code based on different tag values:
This code displays different HTML for different tags. Add
elseifstatements as needed.name === 'tag-1' ) : ?> <p>Display HTML for posts tagged with 'tag-1'.</p> name === 'tag-2' ) : ?> <p>Display HTML for posts tagged with 'tag-2'.</p>Skip to note 15 content
hearvox
Function to show tags in a dropdown:
'; foreach ( get_the_tags() as $tag ) { echo '<option value="' . $tag->name . '">' . $tag->name . "</option>n"; } echo '</select>'; } ?> <h2></h2> <form id="tags-select" class="tags-select" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get"> <input type="submit" name="submit" value="view" /> </form>Skip to note 16 content
tbluhm
Loop with tag post by ID and Link to -> /tag/slug:
// GET TAGS BY POST_ID $tags = get_the_tags($post->ID); ?> <ul class="cloudTags"> <li> <a class="btn btn-warning" href="<?php bloginfo('url');?>/tag/<?php print_r($tag->slug);?>"> name); ?> </a> </li> </ul>