Getting Ready for WordPress 2.7: Really Simple Category Styling
With upcoming WordPress 2.7, just released in beta 3 as I'm writing this, styling your categories gets even easier. Previously, you more or less had to do a conditional tag thing and echo a specific class to get some custom styling, but no more.
Enter this little line of code:
-
<?php post_class(); ?>
The only thing it does, when it's inserted in your post's div tag, is write class="post". In other words, you replace your class="post" with post_class().
Why? Well, this little thing adds some other nice classes to each post as well. We've already discussed the sticky post class, sticky, in a previous article here on Devlounge. Besides that one, it adds the following classes:
category-Xwhere X is the post's category, one for each category, so it could becategory-gamesand category-sports if the posts belong to your games and your sports category.tag-Xwhich works exactly the same ascategory-X, but for tags. So the classtag-monkeyswould be added for a post tagged with "monkeys".
So let's say I want to style my games category differently from my other categories. Simple! Just add .category-games to your stylesheet, and style away. Just for the sake of it, let's make all the text green, 'cause I'm in a green period right now:
-
.category-games { color: green; }
No, I wouldn't write "green" normally, but then again I wouldn't do all the type green either, so...
Anyway, that's about it. Just replace your regular class="post" part in your divs holding your posts, with post_class(). Then you'll have sticky post support, and more!
But wait, there's more! I sometimes want to style some parts of my blog differently, especially single posts. That's what the single.php template is for in a theme, as you know. I usually control the styling of these single posts by adding a class, appropriately named single. When using post_class() I won't be able to do that.
Wrong! Just add the class you want to add to the ones WordPress already appends to the post, like this in my case:
-
<?php post_class('single'); ?>
That will spit out all those category and tag classes, along with a microformat one, and a the one for sticky posts should it be a post marked as sticky, as well as the single class I added.
There you have it. Now get your themes ready for WordPress 2.7 with this lovely new post classes functionality.




Want an avatar? Get a gravatar! • You can link to this comment
I’m wondering if this new feature could be used to make a category icon via CSS styling, versus using the category icon plugin. Any thoughts on how to proceed here?
Want an avatar? Get a gravatar! • You can link to this comment
where should I enter that “little line” ?
Want an avatar? Get a gravatar! • You can link to this comment
Some keyword instruments that may be utilizable in estimating demand are WordTracker and Yahoo Overture’s Term Suggestion Tool. If you’re looking to oversee both of these manually, then I recommend visiting Google Webmaster Tools and Yahoo.