get_the_tags( int $id )

Retrieve the tags for a post.


Description Description


Parameters Parameters

$id

(int) (Required) Post ID.


Top ↑

Return Return

(array|false|WP_Error) Array of tag objects on success, false on failure.


Top ↑

Source Source

File: wp-includes/category-template.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' ) );
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.3.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 3 content
    Contributed by marzian

    // Show post tags with link and a custom separator

    function show_tags()
    {
        $post_tags = get_the_tags();
        $separator = ' | ';
        if (!empty($post_tags)) {
            foreach ($post_tags as $tag) {
                $output .= '<a href="' . get_tag_link($tag->term_id) . '">' . $tag->name . '</a>' . $separator;
            }
            return trim($output, $separator);
        }
    }
  2. Skip to note 4 content
    Contributed by hearvox

    Example using post ID to get tags:
    This doesn’t need to be in The Loop.

    $post_tags = get_the_tags( 24 );
    
    print_r( $post_tags );
    
    /*
    This above prints the tag objects for post ID #24 (if post has any tags):
    Array
    (
        [0] => WP_Term Object
            (
                [term_id] => 108
                [name] => tag-1
                [slug] => tag-1
                [term_group] => 0
                [term_taxonomy_id] => 109
                [taxonomy] => post_tag
                [description] => 
                [parent] => 0
                [count] => 1
                [filter] => raw
                [object_id] => 24
            )
    
        [1] => WP_Term Object
            (
                [term_id] => 109
                [name] => tag-2
                [slug] => tag-2
                [term_group] => 0
                [term_taxonomy_id] => 110
                [taxonomy] => post_tag
                [description] => 
                [parent] => 0
                [count] => 1
                [filter] => raw
                [object_id] => 24
            )
    
    )
    */
    
  3. Skip to note 5 content
    Contributed by hearvox

    Execute code based on different tag values:
    This code displays different HTML for different tags. Add elseif statements as needed.

    <?php
    $post_tags = get_the_tags();
    foreach( $post_tags as $tag) :
    	if ( $tag->name === 'tag-1' ) :
    ?>
    
    <p>Display HTML for posts tagged with 'tag-1'.</p>
    
    <?php
    	elseif ( $tag->name === 'tag-2' ) :
    ?>
    
    <p>Display HTML for posts tagged with 'tag-2'.</p>
    
    <?php
    	else :
    	// Post has neither tag, do nothing.
    	endif; 
    endforeach;
    ?>
    
  4. Skip to note 6 content
    Contributed by hearvox

    Function to show tags in a dropdown:

    <?php
    function dropdown_tags(){
    	echo '<select name="tag" id="tag" class="postform">';
        foreach ( get_the_tags() as $tag ) {
            echo '<option value="' . $tag->name . '">' . $tag->name . "</option>\n";
        }
        echo '</select>';
    }
    ?>
    <h2><?php _e( 'Tags:', 'textdomain' ); ?></h2>
    <form id="tags-select" class="tags-select" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">
        <?php dropdown_tags(); ?>
        <input type="submit" name="submit" value="view" />
    </form>
    
  5. Skip to note 7 content
    Contributed by tbluhm

    Loop with tag post by ID and Link to -> /tag/slug:

    
                                  // GET TAGS BY POST_ID
                                   $tags = get_the_tags($post->ID);  ?>
     
                                   <ul class="cloudTags">
    
                                        <?php foreach($tags as $tag) :  ?>
    
                                       <li>
    									  <a class="btn btn-warning"
                                              href="<?php bloginfo('url');?>/tag/<?php print_r($tag->slug);?>">
                                                    <?php print_r($tag->name); ?>
                                           </a>	
                                        </li>
                                        <?php endforeach; ?>
    						      </ul>
    
  6. Skip to note 8 content
    Contributed by exonfang

    Display tags with links to tag pages in an unordered list:

    $postTags = get_the_tags();
    if ( ! empty( $post_tags ) ) {
    	echo '<ul>';
    	foreach( $post_tags as $post_tag ) {
    		echo '<li><a href="' . get_tag_link( $post_tag ) . '">' . $post_tag->name . '</a></li>';
    	}
    	echo '</ul>';
    }	
    

You must log in before being able to contribute a note or feedback.