WordPress at a glance

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, it needs to be retrieved from the array and only then displayed.

Works based on: get_the_terms()
✈ 1 time = 0.004595s = very slow | 50000 times = 1.34s = fast | PHP 7.2.16, WP 5.2
Hooks in function
Return

Array/false/WP_Error. Array of tag objects on success, false on failure.

Each object contains:

term_id     - the tag ID
name        - the tag name
slug        - a slug generated from the tag name
term_group  - the group of the tag, if any
taxonomy    - should always be 'post_tag' for this case
description - the tag description
count       - number of uses of this tag, total

Usage

get_the_tags( $id );
$id(int)
Post ID.
Default: 0

Examples

#1 Basic usage

Output the tags' names separated by whitespace.

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

#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 . '" />'; 
	}
}

#3 Output the name of the first tag of the post.

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

#4 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
		}
	}
}

#5 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>";
}

Code of get the tags: wp-includes/category-template.php WP 5.2.1

<?php
function get_the_tags( $id = 0 ) {

	/**
	 * Filters the array of tags for the given post.
	 *
	 * @since 2.3.0
	 *
	 * @see get_the_terms()
	 *
	 * @param WP_Term[] $terms An array of tags for the given post.
	 */
	return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) );
}

Related Functions

From tag: WP Loop

More from category: For posts

No comments
    Hello, !     Log In . Register