Justin Tadlock

Tag descriptions in WordPress 2.8

The ability to write a description for my post tags has been a feature I’ve been patiently waiting for in WordPress. As far as I know, this has always existed for categories and link categories.

What can one do with tag descriptions?

  1. Use them on your tag archives to display more information about that particular tag.
  2. Use them to add a meta description on your tag archives.
  3. Display a specific tag’s description anywhere on your site.
  4. Probably some other cool stuff I haven’t thought of yet.

I’ll be describing the first three techniques in this tutorial. And, if you’re lucky enough to be using the Hybrid theme, this will all be taken care of for you in the next update.

Adding descriptions to your tags

Before we get into using tag descriptions within our theme, we’ll want to actually write some descriptions. Head over to your Post Tags section in your WordPress admin and click on a tag to edit. You’ll be presented with the option to edit the tag name, slug, and description.

Just write something that describes what this tag means to you and your site.

WordPress Tag Description

Adding the tag description to your tag archives

Open your theme’s tag.php file. If your theme doesn’t have a tag.php file, it’s probably not very good and I recommend finding a new one. You might have to rig up your archive.php file if you insist on sticking with it.

There’s typically an area where the tag title is shown using the single_tag_title() function. Beneath that, add this code:

<div class="tag-description">
    <?php echo tag_description(); ?>
</div>

Now, your tag descriptions will be displayed when a visitor is browsing a tag archive.

Using the tag description as your meta description

This technique will be useful for SEO plugin developers and theme developers. Here’s some simple code to add a meta description when viewing a tag archive.

<?php if ( is_tag() && tag_description() ) : ?>
    <meta name="description" content="<?php echo wp_specialchars( strip_tags( tag_description() ), 1 ); ?>" />
<?php endif; ?>

Display a tag description by ID

If you need a specific tag description when not on that tag archive, you’ll need to know the tag’s ID. Once you’ve found that, it’s as simple as adding the code where you want it.

<?php echo tag_description( '100' ); ?>

Have fun with your tag descriptions

Remember, the tag_description() function is only available in WordPress 2.8+, which is currently under development and things could change. For more information on this function, check out the tag_description() page on the WordPress Codex.

I’m also thinking of doing a few other WordPress 2.8-specific tutorials in the next week or two. Several of the new features have been covered by other WordPress blogs, but if you’d like to see a tutorial on anything new written by me, feel free to ask in the comments.