Left Brain/Right Brain - Autism News, Science and Opinion since 2003

  • Join the Facebook Left Brain/Right Brain Network
  • Follow Left Brain/Right Brain on Twitter
  • Favorite Left Brain/Right Brain on Technorati
  • Stumble Left Brain/Right Brain
  • Follow the Left Brain/Right Brain Tumblelog
  • Get new posts as email
  • Subscribe via RSS
01 Nov 2004
  • Author: Kev
  • Comments: 2
Add this post to Furl Add this post to Twitter
Add this post to del.icio.us Add this post to Technorati
Add this post to Facebook Add this post to Stumbleupon
Digg this post Add this post to Newsvine
Buzz this post  

Category Icon for WordPress

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="<?php echo $headclass; ?>" >Some Heading</h1>

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.

Link to this post?

If you want to reference this post in your site, use the code below to link to me from your website.

<a href="http://leftbrainrightbrain.co.uk/2004/11/category-icon-for-wordpress/">Category Icon for WordPress</a>

Comments

2 Responses to “Category Icon for WordPress”

  1. 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?

  2. 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.

Sponsored Links
No win no fee

Autism Family Adventures
Jobs for autism

Last 10 Headlines

Last 10 Comments