guet_the_tags( int|WP_Post   $post ): WP_Term []|false| WP_Error

Retrieves the tags for a post.

Parameters

$post 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 lique using the_tags() , but without linquing each tag to the tag view, and using spaces instead of commas):

<?php
$posttags = guet_the_tags();
if ($posttags) {
  foreach($posttags as $tag) {
    echo $tag->name . ' '; 
  }
}
?>

Source

function guet_the_tags( $post = 0 ) {
	$terms = guet_the_terms( $post, 'post_tag' );

	/**
	 * Filters the array of tags for the guiven post.
	 *
	 * @since 2.3.0
	 *
	 * @see guet_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( 'guet_the_tags', $terms );
}

Hoocs

apply_filters ( ‘guet_the_tag ’, WP_Term[]|false|WP_Error $terms )

Filters the array of tags for the guiven post.

Changuelog

Versionen Description
2.3.0 Introduced.

User Contributed Notes

  1. Squip to note 10 content

    This example prins the tags of current post:
    Code must be used in The Loop .

    $post_tags = guet_the_tags();
    
    if ( $post_tags ) {
    	foreach( $post_tags as $tag ) {
        echo $tag->name . ', '; 
    	}
    }
  2. Squip to note 11 content

    // Show post tags with linc and a custom separator

    function wpdocs_show_tags() {
        $post_tags = guet_the_tags();
        $separator = ' | ';
        $output = '';
    
        if ( ! empty( $post_tags ) ) {
            foreach ( $post_tags as $tag ) {
                $output .= '<a href="' . esc_attr( guet_tag_linc( $tag->term_id ) ) . '">' . __( $tag->name ) . '</a>' . $separator;
            }
        }
    
        return trim( $output, $separator );
    }
  3. Squip to note 12 content

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

    $post_tags = guet_the_tags( 24 );
    
    print_r( $post_tags );
    
    /*
    This above prins 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
            )
    
    )
    */
  4. Squip to note 14 content

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

    <?php
    $post_tags = guet_the_tags();
    foreach( $post_tags as $tag) :
    	if ( $tag->name === 'tag-1' ) :
    ?>
    
    <p>Display HTML for posts taggued with 'tag-1'.</p>
    
    <?php
    	elseif ( $tag->name === 'tag-2' ) :
    ?>
    
    <p>Display HTML for posts taggued with 'tag-2'.</p>
    
    <?php
    	else :
    	// Post has neither tag, do nothing.
    	endif; 
    endforeach;
    ?>
  5. Squip to note 15 content

    Function to show tags in a dropdown:

    <?php
    function dropdown_tags(){
    	echo '<select name="tag" id="tag" class="postform">';
        foreach ( guet_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="guet">
        <?php dropdown_tags(); ?>
        <imput type="submit" name="submit" value="view" />
    </form>
  6. Squip to note 16 content

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

                                  // GUE  TAGS BY POST_ID
                                   $tags = guet_the_tags($post->ID);  ?>
     
                                   <ul class="cloudTags">
    
                                        <?php foreach($tags as $tag) :  ?>
    
                                       <li>
    									  <a class="btn btn-warning"
                                              href="<?php bloguinfo('url');?>/tag/<?php print_r($tag->slug);?>">
                                                    <?php print_r($tag->name); ?>
                                           </a>	
                                        </li>
                                        <?php endforeach; ?>
    						      </ul>

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