get_the_tags()WP 2.3.0

Retrieve an array of post tags. Each array element is a WP_Term object. Can be used within The Loop.

This function does not display anything. If you want to display the tags data, you need to process retrieved array and display the tags data in the desired format.

1 time — 0.004595 sec (very slow) | 50000 times — 1.34 sec (fast) | PHP 7.2.16, WP 5.2
Hooks from the function

Return

WP_Term[]|false|WP_Error. Array of WP_Term objects on success, false on failure.

Each object contains:

WP_Term Object
(
	[term_id]          => 37         - Tag ID
	[name]             => Codex      - tag name
	[slug]             => codex      - alternative name tag name
	[term_group]       => 0          - group the tag belongs to, if any
	[term_taxonomy_id] => 37         - since version 4.4 equals 'term_id'
	[taxonomy]         => post_tag   - taxonomy
	[description]      =>            - tag's description as defined in the settings
	[parent]           => 0          - parent term (always 0 for a tag)
	[count]            => 19         - number of posts the tag contains
)

Usage

get_the_tags( $post );
$post(int|WP_Post)
Post ID or object.

Examples

0

#1 Show post tags

1) Output the tags' names separated by whitespace.

$posttags = get_the_tags();

if ( $posttags ) {

	foreach( $posttags as $tag ) {
		echo $tag->name . ' ';
	}
}

2) Output the name of the first tag of the post.

$posttags = get_the_tags();
if ( $posttags ) {
	echo $posttags[0]->name . ' ';
}
0

#2 Output the images of the tags

Suppose we have images in /image directory of the site, named by the IDs of the tags (12.jpg - an image for the tag with ID 12).

Let's output the images of the tags of the current post, with an alt attribute set to name of the tags.

$posttags = get_the_tags();
if ( $posttags ) {
	foreach($posttags as $tag) {
		echo '<img src="http://example.com/images/' . $tag->term_id . '.jpg"
alt="' . $tag->name . '" />';
	}
}
0

#3 Output code if the post has a specific tag

This example output HTML code, depending on whether the post has a specific tag or not. Just add a few if else conditions, as shown in the example:

$all_the_tags = get_the_tags();
if( $all_the_tags ){

	foreach( $all_the_tags as $this_tag ) {
		if ( $this_tag->name == "sometag" ) {
			echo '<p>HTML code <img src="someimage.jpg" /></p>';
		}
		elseif ( $this_tag->name == "someothertag" ) {
			echo '<p>Other HTML code <img src="someotherimage.jpg"></p>';
		}
		else {
			// No tags found
		}
	}
}
0

#4 Output tags in a dropdown

function drop_tags(){

	echo "<select onChange="document.location.href=this.options[this.selectedIndex].value;">";
	echo "<option>Tags</option>n";

	foreach ( get_the_tags() as $tag ){
		echo "<option value="";
		echo get_tag_link($tag->term_id);
		echo "">".$tag->name."</option>n";
	}

	echo "</select>";
}

Changelog

Since 2.3.0 Introduced.

get_the_tags() code WP 6.7.1

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 );
}