Retrieves the tags for a post.
Parameters
$post
int|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. |
Print only the first tag name:
This example prints the tags of current post:
Code must be used in The Loop.
// Show post tags with link and a custom separator
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: