An unfortunate downfall of WordPress’s the_category() is that one cannot define what comes before and after each category.

You can define the separator argument, but what about what comes before each item? Or what if we wanted to get really fancy and apply different styles to different categories? That’s where get_the_category() comes in.

get_the_category() will return an array containing all the categories a specific post was posted in. By using member variables, we can define exactly what happens to the members of this array.

Member Variables

There are five different member variables:

  • cat_ID: This will return the ID number of a specific array member.
  • cat_name: This will return the name of a specific array member.
  • category_nicename: This will return the name of a specific array member, only it will be in a format suitable for URIs (basically, a slug).
  • category_description: This will return any description you’ve specified for any specific category.
  • category_parent: This will return the ID for the category parent of your current category. If there are no parents, a 0 will be returned.

Setting up a loop

We’re more than likely going to want to set up a loop so that we can handle each member of this array. We’ll set it up like this:


foreach((get_the_category()) as $cat) {
//do stuff
}

Make sure that this is inside “The Loop,” or the main loop that your post is contained within.

Now inside this new foreach loop we get to do the fun stuff. Lets take a look at a couple different things we could do with this.

Can somebody say, a list?

Probably one of the most simple things we could do with this is display a post’s categories in a list. Inside the loop we can place the following code:


echo '<li><a href="http://www.elliotswan.com/category/'
. $cat->category_nicename . '">' . $cat->cat_name .
'</a></li>';

This will make a link to that category page using category_nicename, and the text inside that link will be shown via cat_name. The coolest thing about this? It’s in a list!

Now, notice we have no <ul></ul> here. That’s because this needs to be placed outside the foreach loop so that it does not repeat for each list item. Our final code will look like this:


<ul>
<?php foreach((get_the_category()) as $cat) {
echo '<li><a href="http://www.elliotswan.com/category/'
. $cat->category_nicename . '">' . $cat->cat_name .
'</a></li>';
}
?>
</ul>

Now time for something a little cooler.

My Categories have color coordination

We’ll start with the same code we used inside our foreach loop last time.


echo '<li><a href="http://www.elliotswan.com/category/'
. $cat->category_nicename . '">' . $cat->cat_name .
'</a></li>';

Now, let’s add some IDs based on the type of category.


echo '<li class="category-$cat->cat_ID">
<a href="http://www.elliotswan.com/category/'
. $cat->category_nicename . '">' . $cat->cat_name .
'</a></li>';

Pretty simple. Now we can check our WP admin panel to see what IDs are associated with each category (go to yourdomain.com/wp-admin/categories.php). Say, for example, that the category CSS has the ID of 3. And say we want CSS to show up red in our category list. Then in our CSS, we can do something like this:


li.category-3 {
color: red;
}

Pretty sweet, eh?

That’s just two things you can do with this. Use your imagination, have fun, and hopefully cool things will come of it. swan