How to add a tag description to a WordPress tag template page

H

A WordPress tag is an organizational tool built into WordPress’s framework. Tags are essentially keywords that you use to “tag” your post. For instance, this post is organized in my blog based on its blog post category (perhaps one or two categories apply) and many tags. Blog post categories are typically broad, whereas WordPress tags are assigned in broad and narrow categories.

It’s essential to understand the tag WordPress hierarchy of PHP templates. For tags, WordPress uses the first found template in your Theme’s directory (from the list below) in the following order, starting at number 1:

  1. tag-slug.php
  2. tag-id.php
  3. tag.php
  4. archive.php
  5. index.php

Instructions

Open or create the appropriate PHP template file from the list above in your child theme.

To apply a custom layout for all tag-specific pages, modify or create the tag.php file. Use of the tag.php file will allow you to create a similar template to other archive pages, such as the display your blog uses for categories but also will enable you to retain tag-specific layout customization options. As a starting point for your tag.php file, you can copy and paste the current code found in your archive.php template.

Code to display a WordPress tag’s description

The PHP and HTML to display your tag’s description are shown in the below code block.

<?php if ( tag_description())  : // Show an optional tag description ?>
<h2 class="tag-description"><?php echo tag_description(); ?></h2>
<?php endif; ?>

This code is wrapped in an if statement and means, “if there is a tag description, then display the tag description as an H2 with the class tag-description.”

In the case that your tag does not have a tag description, essentially, if there is no tag description to display, then this WordPress tag description snippet of code is ignored.

Where do I create, read, update, or delete a tag’s description?

Create, Read, Update and Delete tags in WordPress from Dashboard > Posts > Tags. The column shown in the below image labeled Description is the text shown by the variable tag_description.

The following PHP code displays the tag’s description in your HTML.

<?php echo tag_description(); ?>
// This line of code displays the description for each tag
Create, Read, Update and Delete tags in WordPress under Posts > Tags
Create, Read, Update and Delete tags in WordPress under Posts > Tags

The website screenshot below highlights an example of a WordPress theme’s tag.php template file displaying a tag’s description on each tag’s archive page. The tag description can be shown as a heading, such as an H2, or a paragraph <p> tag.

An example of a page using a tag.php file with a tag title and description displayed.
https://artsandbricks.com/tag/3d-animals/

I hope this post helps you customize your tag.php archive page in WordPress.

Happy WordPress Coding!

About the author

Kelly Barkhurst

Designer to Fullstack is my place to geek out and to share tech solutions from my day-to-day as a designer, programmer, and business owner (portfolio). I also write on Arts and Bricks, a parenting blog and decal shop that embraces my family’s love of Art and LEGO bricks!

Add comment

By Kelly Barkhurst

Recent Posts

Archives

Categories