Today we’ll learn How to add Tag Support by hacking your WordPress theme!
This is day #15 of 30 WordPress Hacks in 30 Days.
If you’re using WordPress 2.3x or 2.5 or higher you have the ability to use “tags”. If you use social media at all, you should already know what a tag is. Basically it’s just a piece of metadata, a “descriptor” of some kind. When you blog, it’s also another way to pigeonhole and file your content. The implementation of tags in WordPress is very important, because it keep people from “using categories as tags” as many of us have done over the years. There were dozens of plugins available to add tags to your WordPress blog, but now all that functionality is available without using any plugins at all.
Think of categories as high level descriptions. What do you like to blog about? Movies, Music, Politics, and Web Design. Those are categories. However, when you blog – the tags would be Obama, John Candy, AC/DC, and HTML. Get it? All you have to do is enter description tags when you blog, along with a category. Now you’re probably wondering, what do I do with that information?
Depending on how old your theme is it may or may not have “tag support”, which means that it might not be coded to do anything at all with those tags you’re entering. The first thing you can do is to add a (now famous) “tag cloud” in your side bar like I did, with this code:
If you want to know more about how that codes works and how you can modify it, visit the WordPress Codex Tag Cloud Page.
Another thing you can do is to add the tags you used beneath your post by adding some code to your single.php file (for single posts). You could do it on your index.php home page as well, but I don’t much see the point in that. All you have to do is add the following code near the end of the loop to display the tags used at the end of the post:
Tags: ', ', ', ''); ?>
So, now we’ve talked about how to modify your theme to add the tags used after a post, and how to place a tag cloud in your sidebar (without using a plugin). Click on one of the links in my post (to a tag) or my tag cloud and you’ll see (much like a category page) it goes to a “tag” page, or something like site.com/tag/wordpress-hack.
Be default WordPress will look for the “tag.php” file (that most themes don’t have), and if there isn’t one your archive.php will be used by default. You could just leave this as-is, or create your own custom “tag.php” to make your tag pages unique. One way to do this would be to add a “tag cloud” to the top of each tag page. The WordPress Codex Tag Page has more information on creating a custom tag.php file for your theme.
This post was all about “how to add tag support to your WordPress theme”. In a future post, I’ll show you what you need to know about tagged pages and WordPress SEO. As always, if you have something to add to make this post better, or a question – comment now!