get_the_tags() WordPress Function
The get_the_tags() function in WordPress allows you to retrieve the tags assigned to a post. This is useful if you want to display a list of tags for a post on your website.
get_the_tags( int|WP_Post $post_id ) #
Retrieves the tags for a post.
Parameters
- $post_id
(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
function get_the_tags( $post_id = 0 ) { $terms = get_the_terms( $post_id, '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 ); }
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
2.3.0 | Introduced. |