Skip to content

Instantly share code, notes, and snippets.

@jayseventwo
Created June 4, 2014 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayseventwo/ad270505f4f6bae125e6 to your computer and use it in GitHub Desktop.
Save jayseventwo/ad270505f4f6bae125e6 to your computer and use it in GitHub Desktop.
Add category class to body in WordPress
// add category class to body
function body_class_add_categories( $classes ) {
// Only proceed if we're on a single post page
if ( !is_single() )
return $classes;
// Get the categories that are assigned to this post
$post_categories = get_the_category();
// Loop over each category in the $categories array
foreach( $post_categories as $current_category ) {
// Add the current category's slug to the $body_classes array
$classes[] = 'category-' . $current_category->slug;
}
// Finally, return the $body_classes array
return $classes;
}
add_filter( 'body_class', 'body_class_add_categories' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment