The get_the_tag_list() WordPress PHP function retrieves the tags for a post formatted as a string.
Usage
get_the_tag_list($before, $sep, $after, $post_id)
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.
More information
See WordPress Developer Resources: get_the_tag_list()
Examples
Basic Example – Outputting Tags in a Paragraph
This example outputs the list of tags inside a paragraph, with tags separated by commas.
echo get_the_tag_list('<p>Tags: ', ', ', '</p>');
Output:
<p>Tags: <a href="tag1">Tag 1</a>, <a href="tag2">Tag 2</a></p>
Complex Example – Outputting Tags in an Unordered List
This example checks if the post has any tags, and if there are, outputs them to an unordered list.
$tag_list = get_the_tag_list('<ul><li>', '</li><li>', '</li></ul>'); if ($tag_list && !is_wp_error($tag_list)) { echo $tag_list; }
Output:
<ul> <li><a href="tag1">Tag 1</a></li> <li><a href="tag2">Tag 2</a></li> </ul>
Outputting Tags with Custom Separator
This example outputs the list of tags with a custom separator (a pipe symbol).
echo get_the_tag_list('<p>Tags: ', ' | ', '</p>');
Output:
<p>Tags: <a href="tag1">Tag 1</a> | <a href="tag2">Tag 2</a></p>
Outputting Tags for a Specific Post
This example outputs the list of tags for a specific post with ID 123
.
echo get_the_tag_list('<p>Tags: ', ', ', '</p>', 123);
Output:
<p>Tags: <a href="tag1">Tag 1</a>, <a href="tag2">Tag 2</a></p>
Outputting Tags with Custom HTML Markup
This example outputs the list of tags with custom HTML markup, such as adding a class to the tag links.
echo get_the_tag_list('<div class="tag-list">Tags: <span class="tag">', '</span><span class="tag">', '</span></div>');
Output:
<div class="tag-list">Tags: <span class="tag"><a href="tag1">Tag 1</a></span><span class="tag"><a href="tag2">Tag 2</a></span></div>