get_the_tag_list()WP 2.3.0

Gets the HTML string of tags for the current post. The name of each tag will be a link to the archive of posts with that tag. Used in a loop.

In the parameters passed to the function, HTML tags can be used.

This function does not output anything to the screen, but only retrieves a string for further processing. If you need to display tags on the screen, use the_tags().

Used By: the_tags()
1 time — 0.004159 sec (very slow) | 50000 times — 3.69 sec (fast) | PHP 7.1.2, WP 4.7.3
Hooks from the function

Returns

String|false|WP_Error. Links of the tags for the current post, separated by the $sep parameter.

Usage

get_the_tag_list( $before, $sep, $after );
$before(string)
Initial text. HTML tags can be used.
Default: ''
$sep(string)
Separator between links.
Default: ''
$after(string)
Closing text. HTML tags can be used.
Default: ''

Examples

0

#1 Outputs comma-separated tags within a paragraph:

echo get_the_tag_list( '<p>Tags: ',', ','</p>' );

The result will be something like this:

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

#2 Checks if the post has tags and if so, displays them in the UL list:

if( get_the_tag_list() ){
	echo get_the_tag_list( '<ul><li>', '</li><li>', '</li></ul>' );
}

You end up with something like this:

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

You can add CSS classes and styles, if necessary.

Changelog

Since 2.3.0 Introduced.

get_the_tag_list() code WP 7.0

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 );
}