An even better body_class for WordPress
by Moises Kirsch
Here is a little update to my better body_class code:
add_filter('body_class','better_body_class');
function better_body_class($classes){
global $post, $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;
if($is_lynx){
$classes[] = 'lynx';
$classes[] = 'notie';
}
elseif($is_gecko){
$classes[] = 'gecko';
$classes[] = 'notie';
}
elseif($is_opera){
$classes[] = 'opera';
$classes[] = 'notie';
}
elseif($is_NS4){
$classes[] = 'ns4';
$classes[] = 'notie';
}
elseif($is_safari){
$classes[] = 'safari';
$classes[] = 'notie';
}
elseif($is_chrome){
$classes[] = 'chrome';
$classes[] = 'notie';
}
elseif($is_IE){
$classes[] = 'ie';
}
elseif($is_iphone){
$classes[] = 'iphone';
$classes[] = 'notie';
}
else $classes[] = 'unknown';
$other_classes = explode('/', get_page_uri($post->ID));
return array_merge($classes, $other_classes);
}
So what does this thing do?
It adds a the slug of the current page to the class just like the old one but it also combines Nathan Rice code for browser detection with even a little extra class called notie a.k.a. Not Internet Explorer.
The notie class is very useful when you are debugging your CSS and trying to fix all the annoying things that IE doesn’t get right.
Comments
great filter…. any idea how to also add the category slug from the permalink as a body class as well, perhaps preceeding the post name?
I’m trying to get the slug as a body class on single post pages.
Thanks,
Michael
You can achieve it adding the function get_the_category();
To tell you the exact code I would need to check the codex and update my code.
Sounds like a good idea to add the category so look forward for an updated function in a few days.