get_the_tag_list( string $before = '', string $sep = '', string $after = '', int $post_id )

Retrieves the tags for a post formatted as a string.


Parameters Parameters

$before

(string) (Optional) String to use before the tags.

Default value: ''

$sep

(string) (Optional) String to use between the tags.

Default value: ''

$after

(string) (Optional) String to use after the tags.

Default value: ''

$post_id

(int) (Optional) Post ID. Defaults to the current post ID.


Top ↑

Return Return

(string|false|WP_Error) A list of tags on success, false if there are no terms, WP_Error on failure.


Top ↑

Source Source

File: wp-includes/category-template.php

function get_the_tag_list( $before = '', $sep = '', $after = '', $post_id = 0 ) {
	$tag_list = get_the_term_list( $post_id, 'post_tag', $before, $sep, $after );

	/**
	 * Filters the tags list for a given post.
	 *
	 * @since 2.3.0
	 *
	 * @param string $tag_list List of tags.
	 * @param string $before   String to use before the tags.
	 * @param string $sep      String to use between the tags.
	 * @param string $after    String to use after the tags.
	 * @param int    $post_id  Post ID.
	 */
	return apply_filters( 'the_tags', $tag_list, $before, $sep, $after, $post_id );
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.3.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    A Slightly More Complex Example

    This checks if the post has any tags, and if there are, outputs them to an unordered list.

    /** @var string|false|WP_Error $tag_list */
    $tag_list = get_the_tag_list( '<ul><li>', '</li><li>', '</li></ul>' );
    
    if ( $tag_list && ! is_wp_error( $tag_list ) ) {
    	echo $tag_list;
    }
    

    This will return something in the form:

    <ul>
    	<li><a href="tag1">Tag 1</a></li>
    	<li><a href="tag2">Tag 2</a></li>
    	...
    </ul>
    

    You can add classes and styles with CSS, as necessary.

  2. Skip to note 2 content
    Contributed by Codex

    A Basic Example
    This outputs the list of tags inside a paragraph, with tags separated by commas.

    echo get_the_tag_list( sprintf( '<p>%s: ', __( 'Tags', 'textdomain' ) ), ', ', '</p>' );
    

    This would return something like:

    <p>Tags: <a href="tag1">Tag 1</a>, <a href="tag2">Tag 2</a>, ...</p>
    

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