Apparently in the version of WordPress I’m using, the ability to easily pass the category_id to the index page is broken. This is a shame because I needed it to display an icon for the category each post belongs to. A little judicious searching reveals the
get_the_category()
function, which you can extract the category_id from easily. This is how I did it:
So, we basically treat the function as an array and extract the appropriate key – in this case
category_id
From this point on, because I have a fixed number of categories that I know will never change, I compare the values and set a variable called
$headclass
which I then use in my tag attribute like so:
<h1 class="" >Some Heading
So, its now a simple matter to assign a style declaration for each possible category icon:
If you wanted to make this more dynamic (if you regularly added and removed categories for example) you could rename your images and classes for each category id number.
I’ve found interesting your approach to category and images. I’ve got a troubles I’ve fallen into that I would like to solve.
How can I associate an icon to each category in my category listing (on the right of my side menu)?
I’ve searched for hacks or examples but i’ve found nothing.
I would like to associate a specific icon to each category, already in the category listing.
may you help me about it?
Hi Thomas,
I never found an answer to this either. I did see some hacks in the WordPress forums but they didn’t work for me.
In the end I decided to hardcode the image in the links for my category menu – not an ideal solution but all I could think to do in the end.
Sorry to be the bearer of bad news.