Support » Themes and Templates » How to add Category Icons?

  • I would like to add little icons on each post that represent the category that post is in, like what you can do with Geeklog or PHP Nuke. I did a search of these forums and found that there was a simple hack for adding category icons (, and I tried that hack, which involved adding this code to the index.php file:

    <img src="<?php the_category() ?>.gif" alt="<?php the_category() ?>">

    This did not work. I followed the directions of the hack exactly and uploaded my images in the right place and was careful to get my upper/lower case letters correct because the file names are case sensitive. However, instead of an image, I got a broken image and the category name beside a bullet, and .gif"> trailing off to the side. I don’t understand why this hack didn’t work and I decided to start a new thread because that old thread about category icons hasn’t been updated since october. Could someone please tell me if it’s possible to add Category Icons, and if so, how? Thank you SO much!

Viewing 12 replies - 1 through 12 (of 12 total)
  • are you using the friendly urls? (ones that look like this:
    I found that this can “break” image links unless the image has the full path.
    If your images are in an image folder in the root, you might find this works…
    <img src=”/images/<?php the_category() ?>.gif” alt=”<?php the_category() ?>”>

    The problem is that the_category() returns an unordered list of categories, not just the name. While that can be easily changed with a parameter, there’s also the fact that multiple categories for any given post would mess things up.

    TechGnome, I tried moving the images into a separate folder but it still does not work. Jason, what parameter needs to be changed to make category images work? Thanks!

    Might be so simple that it’s stupid, but I just entered a <img src> tag in place of an actual “name” for the category. Seems to work pretty good.

    Have you tried numbering the icons and using the_category_ID?
    On second thoughts, I’m not 100% sure the_category_ID still exists – haven’t tested it lately – so try inserting a comma in the parameters for the_category.

    WillM, what do you mean by adding a comma? I mean, how would I write that out? I am not very seasoned in PHP, sorry! Thanks everyone for all your help!

    My suspiscion was correct: support for multiple categories seems to have eliminated the_category_ID:
    I tried to see whether it was still in the Wiki but I couldn’t access the page.

    Oh, ok. Thanks for all your help again!! 🙂

    I wrote a function that allows you to do this with multiple categories, it’s available here:

    sugg: why not use the upload function to add some images in the directory we choose, and once it’s done, in the category creation page we can select inside the list of image the one chosen for that category… a simple drop-down listing the content of the upload directory can do the trick… once it’s selected, it’s entered in the category table (we have to add a field then) … this would make the image available only if there is one entry in the category table…

    Instead of using complex hack code, I chose to simply search the query string for the category number, then I’ve inserted it inline where needed to modify image or css tags. I’m no PHP guy, but this works great for me and it’s one line of code:
    <?php echo($cat); ?>


Viewing 12 replies - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.