Very Easy WordPress Theme Hack: Show Category Images

Posted by jtpratt |04 Jan 08 | 67 comments

I’m going to show you how to add some simple PHP code to your WordPress template and make linked images appear instead of text for your categories in posts. All you need to know is simple PHP, some basic HTML, and how to make your own graphics.

**UPDATE** May 2008
As the last comment at the end of this page shows there is now a Category Icons WordPress Plugin you can download to install to manage your category icons right in your dashboard. If you prefer to hack your theme, the code below will work just fine! The cool thing about the plugin is that they give you ways to do one or multiple category icons, but also a methods for putting icons with your categories in the side bar as well! I’m always very grateful when a good plugin come along! Now back to the original post…

Have you ever wanted to hack WordPress and just make it do something you always wanted it to do? I think of things I want it to do all the time but don’t always have the hours to devote to finding the answer. I’ve searched for PHP code snippets for WordPress and tutorials and how-to articles as well and haven’t seen much that really helped me. Until today – when I read the tutorial page at WPDesigner today. I didn’t get exactly the code I needed, but pretty close. I just needed to hack it a bit to get it to do what I wanted.

First, this WordPress tutorial hack assumes a few things.

  1. You’ve modified your WordPress template code before
  2. You can modify simple PHP
  3. You can write simple HTML
  4. You can make your own graphics

First things first – here’s the code:


Posted in:

category_nicename; echo ""; echo "\"$catname\n"; } ?>

I hacked this code from this tutorial page on WPdesigner. The code they had was only good to display one category per post. I hacked it because my blogs all have posts assigned to multiple categories. Also, I wanted the images to all be linked so people can just click them and go right to that category page. Let’s look at the pieces of this code you might need to modify.

In line 1, you might want change “Posted in:” to alternate text like “Filed under:”, etc.

In line 5, if your permalink structure is different than the default, you may need to change /category/ to what you are using.

In line 8 change /wp-content/cat-icons/ to wherever you will be storing your category icons in your site.

In line 10, if you are using gif’s or jpg’s change .png to your graphic file format.

Now all you have to do is create one image for every category you have an place them in the folder you just created on the server. Make sure that the graphics are named exactly as your categories, and if your categories have multiple words just add a hyphen in between each word. That’s it! All you have to decide now is where you want these category icons to appear, before or after your post – and add them into the appropriate place. I placed mine just after the title of the post on my single post pages – as you can see at the top of the post on this page. You can hack this category icon code a hundred other ways to do what you want. I think in the future I’m going to use it to replace the text category links in my sidebar. The only caveat to this (and the code hack above) would be the fact that if you don’t have an image a text link will display (in FF, or broken image in IE). So – if you create new categories in the future, make sure you create a new image for each.

My Blogging Mistake for this post is that I didn’t decide to customize and hack my WordPress Theme before now to create my own layout. Do you have a question or a new way to hack up this wordpress category icon PHP code? Comments and post it now!


67 Responses

Leave a Reply