Over the past couple of months I’ve been thinking I really need to overhaul my categories on this blog and replace them with tags. Tags match my mental habits better (aka: I do love me data about data) and would help with one-off posts, etc. However, I wasn’t looking forward to dealing with another WordPress plugin, etc. Today I decided to bite the bullet and just do it. I fired up Google and the first thing I notice is that, guess what? WordPress just added in tag functionality with version 2.3, released 5 days ago. I’m out of it for a while and suddenly everyone gets delusions of grandeur. Of course, it’s not just that simple. If you’re using a custom theme, you’re going to have to make some (easy) changes.
Let’s start off with the usual cautionary tale: 2.3 is a pretty big update to WordPress. Be a little wary updating a critical site. Be really wary if you need a plugin that has anything to do with categories, as the plugin will probably need a new version. For example, I use Google XML Sitemaps Generator and it caused database errors to occur while editing this post. Upgrading to the latest version of the plugin fixed it, but it’s worth comparing your active plugins to the list of known compatible ones.
Right, then. Assuming you agonized over the decision (aka: ignored the last paragraph and clicked “upgrade”) and are running 2.3, you should see a “Tags” textbox right below the main Post textbox when you’re editing or writing a post. If you’re this far then you can add tags to the post, but your readers probably won’t see them because the theme doesn’t know how to display tags. Now, if you’re using a 3rd party theme, especially one of the defaults, just get the latest version and you’re probably good to go. If you’re one of the Legion Of Tinkerers, then a simple update or two is needed.
You’re probably going to need to change two files: index.php and single.php. Index.php is what readers see when they first hit your blog, it probably shows at least the introductory excerpts for several posts. Single.php is what readers see when they click on a post either to look at comments or if you’re using the More tags to only show a bit of the post on the index. Chances are your layout is the same in both, so you’re going to need to decide where you want to list the tags and then make the same change in both.
The code you need to add is:
< ?php the_tags('text before', 'separator', 'text after'); ?>
For example, I wanted to add a line that said “Tags:” and then listed each tag, separated by a comma (but not a space) and then put a long dash after it. I wanted this right before where I currently list categories. As such, I cracked open my index.php, and looked for < ?php the_category(',') ?>
. Once I found it, I just went to the left of that and any text that was dealing with categories (mine says “Filed under”). Then I added:
< ?php the_tags("Tags:", ',', ' — '); ?>
I did the same thing to single.php and voila! If there are tags with the post (right now just this post), then the text “Tags:tag1,tag2,tag3 — ” is displayed. The hex code at the end is a long dash that I use to visually separate this from categories.
Easy!
The next thing you may want to do is display one of those nifty tag clouds (like Flickr and all the other cool tag-kids do). I threw mine on the sidebar. Again, the code to add is very simple:
< ?php wp_tag_cloud('smallest=8&largest=18&number=25'); ?>
Notice I pre-populated some things here. By default, the tag cloud will size from font 8 to 22 and display 45 tags at once. I wanted to limit the biggest size a little, and display fewer tags, so I made the above changes. If you like the default, just use wp_tag_cloud('');
and you’re good. Keep in mind that, if you’re modifying the sidebar, you’ll probably need to hit sidebar.php and maybe sidebar-single.php and sidebar-pages.php if you have them. Assuming you want the tag cloud to show when you display one page (or post) at a time, of course. If you want to play around more with it, check out the tag cloud entry in the Codex.