Skip to content

Instantly share code, notes, and snippets.

@ivanhoe011
Created January 10, 2016 15:18
Show Gist options
  • Save ivanhoe011/fb076bbb52a106401315 to your computer and use it in GitHub Desktop.
Save ivanhoe011/fb076bbb52a106401315 to your computer and use it in GitHub Desktop.
WP: Add category classes to <body>
// ...
add_filter('body_class','iii_inject_category_classs');
// add post's categories as css classes to <body> (with cat- prefix added)
function iii_inject_category_classs($classes) {
global $post;
if ($post and is_single($post)) {
foreach((get_the_category($post->ID)) as $cat) {
$classes[] = 'cat-' . esc_attr($cat->category_nicename);
}
}
return $classes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment