• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

Vi Wickam

Loveland Colorado Online Marketing, Website Design and Ecommerce web development

  • Home
  • Meet Vi
  • What Vi Does
    • Website Creation
    • Big Picture Online Marketing
  • Blog
  • Contact
September 26, 2014 by Vi Wickam

Add Current Category to WordPress Body Tag

One of the most powerful tools in customizing a set of pages via CSS is custom body tags. If you want to customize the look of all of the posts that belong to a certain category, the easiest way to do it is by adding a custom class to the body tag of every post in that category.

I use the Genesis Theme Framework for almost all of the wordpress sites I develop and most of the Genesis Child Themes already have this functionality built in. If you are not using the Genesis Theme Framework, or are using a theme that doesn’t have category body tags included in the Body tag by default, here is the code that you can use to add the category slug as custom body tag to your posts.

This code should be added to your theme’s functions.php file.

/*Add custom body tag for categories*/
add_filter( ‘body_class’, ‘cat_body_class’ );
function cat_body_class( $classes ) {
if (is_single()) {
$category = get_the_category();
$classes[] = ‘category-‘.$category[0]->slug;
}
return $classes;
}

If you are looking for a general tutorial on how to add custom body tags, Studio Press has a great tutorial on it here:

http://my.studiopress.com/snippets/custom-body-class/

 

Category: WordpressTag: body tag, code, css, functions.php, genesis, html, wordpress

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a Quote

"*" indicates required fields

1
2
Name*
This doesn't need to be an exact budget, but I need an idea of what the approximate budget is that you are planning on for this project.
Give me some more detail about this project so that I can understand what you have in mind and in what areas you would like me to help you out.
This field is for validation purposes and should be left unchanged.

Copyright © 2023 · Wizard of Ads Online · All Rights Reserved · Vi Wickam is a Wizard of Ads Digital Marketing and Strategy Partner