Print All Tags & Categories of a Blog Post

Quick Summary

  • In single.php, loop over get_the_tags and link each tag with get_tag_link, giving you plain markup to style instead of the_tags output.
  • The category block uses get_categories, which lists every category with posts site-wide; use get_the_category for only the current post's categories.
  • A post without tags makes get_the_tags return false and the foreach trigger a PHP warning, so check the result before looping.

These two snippets print the tags and categories of a post as plain links you can style yourself, instead of the default output of the_tags() and the_category(). Add them to your WordPress single post template, such as single.php, inside the loop.

Display the post’s tags

get_the_tags() returns the tags of the current post. Each one is printed as a link to its tag archive, built with get_tag_link(), inside a .single-tags wrapper.

PHP
<div class="single-tags">
   <?php foreach( get_the_tags($post->ID) as $tag ): ?>
   <a href="<?php echo get_tag_link($tag->term_id); ?>" rel="tag">
   <?php echo $tag->name; ?>
   </a>
   <?php endforeach; ?>
</div>

If a post has no tags, get_the_tags() returns false and the foreach triggers a PHP warning. Store the result in a variable and check it before the loop.

Display categories

This block uses get_categories(), which returns every category on the site that has at least one post, not only the categories of the current post. Each one is linked to its archive with get_category_link().

PHP
<div class="single-categories">
   <?php
      $categories = get_categories();
      foreach($categories as $category):
   ?>
   <a href="<?php echo get_category_link($category->term_id); ?>" rel="category tag">
   <?php echo $category->name; ?>
   </a>
   <?php endforeach; ?>
</div>

To list only the categories assigned to the current post, use get_the_category() in place of get_categories(). The rest of the loop stays the same, since both return category objects with term_id and name.

The rel="tag" and rel="category tag" values match what WordPress itself prints for tag and category links. Consider wrapping the names in esc_html() when you print them.

Share on X (opens in a new tab) Share on LinkedIn (opens in a new tab) Share on Facebook (opens in a new tab)

ChatGPT (opens in a new tab) Claude (opens in a new tab) Gemini (opens in a new tab) Perplexity (opens in a new tab) Grok (opens in a new tab)

More Insights

See More