get_the_tag_list( string $before = '', string $sep = '', string $after = '', int $post_id ): string|false|WP_Error
Retrieves the tags for a post formatted as a string.
Contents
Parameters
-
$before
string Optional -
String to use before the tags.
Default:
''
-
$sep
string Optional -
String to use between the tags.
Default:
''
-
$after
string Optional -
String to use after the tags.
Default:
''
-
$post_id
int Optional -
Post ID. Defaults to the current post ID.
Return
string|false|WP_Error A list of tags on success, false if there are no terms, WP_Error on failure.
More Information
This function generates a HTML string of the tags associated with the current post. The name of each tag will be linked to the relevant ‘tag’ page. You can tell the function to put a string before and after all the tags, and in between each tag. Differently from get_the_category_list, this tag must be used inside The Loop.
Source
File: wp-includes/category-template.php
.
View all references
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 );
}
Hooks
-
apply_filters( 'the_tags',
string $tag_list ,string $before ,string $sep ,string $after ,int $post_id ) -
Filters the tags list for a given post.
Changelog
Version | Description |
---|---|
2.3.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
A Slightly More Complex Example
This checks if the post has any tags, and if there are, outputs them to an unordered list.
This will return something in the form:
[html]
<ul>
<li><a href="tag1">Tag 1</a></li>
<li><a href="tag2">Tag 2</a></li>
…
</ul>
[/html]
You can add classes and styles with CSS, as necessary.
A Basic Example
This outputs the list of tags inside a paragraph, with tags separated by commas.
This would return something like:
[html]
<p>Tags: <a href="tag1">Tag 1</a>, <a href="tag2">Tag 2</a>, …</p>
[/html]