WordPress.org

Ready to get started?Download WordPress

Codex

Function Reference/get the tags

Contents

Description

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.

Usage

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 . ' '; 
  }
}
?>

Parameters

$id
(int) (optional) Post id.
Default: None

Examples

Show tag Images

This outputs tag images named after the term_id with the alt attribute set to name. You can also use any of the other member variables instead.

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

Show the First tag Name Only

<?php
$posttags = get_the_tags();
$count=0;
if ($posttags) {
  foreach($posttags as $tag) {
    $count++;
    if (1 == $count) {
      echo $tag->name . ' ';
    }
  }
}
?>

Display code bases on different tag values

This code will display HTML code depending on if this post has a certain tag or tag(s). Just add as many else if statements as you require.

<?php 
if ($all_the_tags);
$all_the_tags = get_the_tags();
foreach($all_the_tags as $this_tag) {
	if ($this_tag->name == "sometag" ) {
?>

<p>SOME HTML CODE <img src="someimage.jpg"></p>

<?php 	} else if ($this_tag->name == "someothertag" ) { ?>

<p>SOME OTHER HTML CODE <img src="someotherimage.jpg"></p>

<?php 	} else {	
		// it's neither, do nothing
?>
		<!-- not tagged as one or the other -->
<?
	}
}
}
?>

Function to show tags in a dropdown

This function outputs 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>";
}

Return Values

(array) 
An array of Tag objects

Member Variables

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

Change Log

Since: 2.3.0

Source File

get_the_tags() is located in wp-includes/category-template.php.

Related

Tags: get_tag(), get_tag_link(), get_tags(), get_the_tag_list(), get_the_tags(), is_tag(), the_tags(), single_tag_title(), tag_description(), wp_generate_tag_cloud(), wp_tag_cloud(), get_the_terms(), wp_get_object_terms()

See also index of Function Reference and index of Template Tags.