get_the_tags( int|WP_Post $post ): WP_Term[]|false|WP_Error
Retrieves the tags for a post.
Contents
Parameters
-
$post
int|WP_Post Required -
Post ID or object.
Return
WP_Term[]|false|WP_Error Array of WP_Term objects on success, false if there are no terms or the post does not exist, WP_Error on failure.
More Information
This function returns an array of objects, one object for each tag assigned to the post. If this function is used in The Loop, then no ID need be passed.
This function does not display anything; you should access the objects and then echo or otherwise use the desired member variables.
The following example displays the tag name of each tag assigned to the post (this is like using the_tags() , but without linking each tag to the tag view, and using spaces instead of commas):
<?php $posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { echo $tag->name . ' '; } } ?>
Source
File: wp-includes/category-template.php
.
View all references
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. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
Print only the first tag name:
This example prints the tags of current post:
Code must be used in The Loop.
Top ↑
Feedback
As $id is a required parameter, in order to get the current post id is recommended to add the get_the_ID() function as follow:
— By giorgiocolombo —
// Show post tags with link and a custom separator
Top ↑
Feedback
Before running “if” statement you should define the $output variable. — By cristim —
Example using post ID to get tags:
This doesn’t need to be in The Loop.
Display tags with links to tag pages in an unordered list:
Execute code based on different tag values:
This code displays different HTML for different tags. Add
elseif
statements as needed.Function to show tags in a dropdown:
Loop with tag post by ID and Link to -> /tag/slug: