Playing with WordPress Categories
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. 












you should post some examples
@blado: Aas in seeing it them in action? I’m using the first one myself (showing them as a list), and the second was more just to show what WP is capable of and to get those creative juices flowing.
Oh wow now adays I just write my own functions for stuff like this. WordPress’ default template functions for categories and posts simply don’t cut it for me.
What I’d really like to do is set up different ads (from different linkshare type affiliate programs) that display on posts depending on their category — so that my music posts only have iTunes ads; my technology posts have Dell ads, etc. This type of conditional formatting could certainly make it happen, but it would be great.. nay.. *amazing* if it could be turned into an elegant plugin.
Hi
Other alprazolam dosage If your need more informarmation download http://alprazolam.10pharm.com
Bye
Hi
Help need? cheapest levitra If your need more informarmation look this http://levitra.10pharm.com
Bye
goood!
After a worst medical emergency, I lost my great credit score. Now what besides time can help me back on my feet? Your suggestions, am trying to improve credit. Is worth it? Have U applied there? Please advise.
fedelity college card
? I Hi there! I’m in search for credit cards with airline miles. There are several offers at three websites. But I chose one site about credit. Is a reliable, please, tell me? Go to
citibank priemier pass
Hi people! I have very bad credit and I tried too many times to improve it! Everything was in vain! Recently I thought why not and applied for an AmEx credit card at some website. And what you think they did? They approved me thickheads!! ’Cause I submitted I get 50k more a year…Wanna have fun? Go to
credit card fica score
Official banks’ sites are not the best place to shop for credit cards. It is much better to check out site that have offers from all major banks and credit card companies. They give you all the necessary information and give you the opportunity to compare. I would recommend to go to
rent to own no credit
Thank you, this was the best explanation to be found, I was desperately looking for this kind of thing.
Greetings Elliot,
wonderful article, I had a question about your site; specifically the fonts you use in conjunction with flash. Very cool indeed!
Is that a plugin, or what?
Thanks!
Thank you.
This is great stuff. I’m working on getting some effect the same as get_the_category() on the sidebar. I want to display most recent posts, most viewed posts, belonging to the category of the current post/category. This shall be done for Discount Shopping @ apmart.com
But so far no hope because even when I declare variable
$category = get_the_category()
inside the loop, when it come to the sidebar it’s totally gone.
Do you have any idea how to solve this problem?
BTW, Do you moderate your blog comments? I see alot of spams on this page.
Thanks in advance.
I recommend to subscribe to RSS place in a conspicuous place! Readers will be more! Especially at a blog, how are you! Tested – a 30% increased the number of subscribers!
Elliot, thanks for the tips. My question is:
<?php foreach((get_the_category()) as $cat) {
echo 'category_nicename . '">' . $cat->cat_name .
'';
}
?>
</code<
If i want to display the child of the categories or category how make it work?
Very thanks in advance!
That’s an interesting problem. Unfortunately, get_the_category doesn’t have a way to do that. However, wp_list_categories does. The problem with wp_list_categories, though, is that it shows everything and not just what the current post is under.
The only way I can see of doing it is by combining the two, so you use get_the_category() to find the post IDs then run those through wp_list_categories. Ugly, but I think it would work.
This article is helping me get very close to what I want to accomplish, thanks. Is there a way to have the li class call the cat_ID of the Parent Category?
[... - http://www.elliotswan.com is other useful authority of advice. Online Car insurance claims [… -
My credit score last year got lower because i have some unpaid bills on my credit card company and i also lost my job.`;;
My credit score last year got lower because i have some unpaid bills on my credit card company and i also lost my job..”;
Interesting info you posted at my blog. I’ll keep an eye on your upcoming posts,Aastha
i am not a fan of having credits and getting credits cards.:’:
in this times of economic recession, sometimes it is difficult to have a great credit score`*~
as much as possible i also mantain a good credit record so that i can do business transactions with ease’::
we can’t avoid to have some credit on banks or from other people, there are just times that we are short of money:.~
at one point in our life we will always have some bad credit because of some unpredictable factors ”
it is quite easy to get a good credit score specially if you know how to micromanage your finances “”
bad credits can happen at any point in your life so be prepared to always get some extra income ,’;
we may always need to do some credit specially if we weant to invest on something “:*
I’m pleased I identified this weblog, I couldnt discover any information on this subject matter prior to. I also run a webpage and if you ever would like to ever significant in a bit bit of guest writing for me if feasible really feel free of charge to let me know, i’m continually appear for many people to test out my webpage. Please quit by and leave a comment sometime!
thanks for your thoughts on this, I felt a bit struck by this article. Thanks again!