the_tags()
and Practical ExamplesThe the_tags()
function in WordPress is used to display the tags associated with a post. It outputs an HTML list of linked tags, making it easy for visitors to explore related content.
the_tags( $before, $sep, $after, $post_id );
$before
(string) – Text/HTML to display before the tags (default: 'Tags: '
).
$sep
(string) – Separator between tags (default: ', '
).
$after
(string) – Text/HTML to display after the tags (default: none).
$post_id
(int) – Optional. The post ID (uses current post if not specified).
Default Output (Comma-separated list)
the_tags();
Output:
Tags: <a href="https://example.com/tag/wordpress">WordPress</a>, <a href="https://example.com/tag/php">PHP</a>
Custom Formatting
the_tags( '<div class="post-tags"><span>Tags: </span>', ', ', '</div>' );
Output:
<div class="post-tags"> <span>Tags: </span> <a href="https://example.com/tag/wordpress">WordPress</a>, <a href="https://example.com/tag/php">PHP</a> </div>
Conditional Check (Display only if tags exist)
if ( has_tag() ) { the_tags( '<p>Related Topics: ', ' · ', '</p>' ); }
Output (if tags exist):
<p>Related Topics: <a href="https://example.com/tag/seo">SEO</a> · <a href="https://example.com/tag/woocommerce">WooCommerce</a></p>
Using with get_the_tags()
for Advanced Control
$tags = get_the_tags(); if ( $tags ) { echo '<ul class="tag-list">'; foreach ( $tags as $tag ) { echo '<li><a href="' . get_tag_link( $tag->term_id ) . '">' . $tag->name . '</a></li>'; } echo '</ul>'; }
Output:
<ul class="tag-list"> <li><a href="https://example.com/tag/wordpress">WordPress</a></li> <li><a href="https://example.com/tag/theme">Theme</a></li> </ul>
Improve SEO: Internal linking via tags enhances site structure.
User Engagement: Helps visitors discover related posts.
Custom Styling: Wrap tags in <div>
or <span>
for CSS styling.
Note: Use has_tag()
to check if a post has tags before calling the_tags()
.
get_the_tag_list()
– Returns tags as a string (for manual echo).
get_the_tags()
– Retrieves tags as an array for custom loops.
This function is ideal for themes, plugins, or custom post templates.
Apply for your exclusive plan for free