Add a Post’s Category Name to Body Class in WordPress
by c.bavota | Posted in Tutorials | 8 comments
Aug 11 2011
I was working with Digibomb on presswork.me and we needed to add the current post’s category name to the body class in order to style the page differently from the others.
All it took was a few lines of code added to the functions.php file:
function add_category_name($classes = '') {
if(is_single()) {
$category = get_the_category();
$classes[] = 'category-'.$category[0]->slug;
}
return $classes;
}
add_filter('body_class','add_category_name');
With that in place, you should now see a new class at the end of your body tag when you are on a single post or single page.



Hi…
this is a cool script.
We have been using it for the author, but implemented directly on the
Is there a way to adapt it to these function?
That is a slick piece of code. I sometimes hate that you are stuck with most theme templates for the entire site. Not only is the code cool, but I am getting all sorts of new ideas of how I can use it to change up my blog based on the categories.
Will be giving it a go, thanks for sharing!
very interesting post.I love your site
This is a pretty cool idea. I will use it in my next project.
Could this be used for custom post type categories? It would be great…
Great work! This is the kind of info that are meant to be shared across the net. Shame on the seek engines for no longer positioning this post upper! Come on over and discuss with my web site . Thanks =)
Thanks, will try it.
Awesome, I’ve been looking around all day for this neat little script. I’ll use it on my design inspiration blog. Thank you so very much. Love the layout and design of Bavotasan.com
I’ll redesign my blog as well