Is there a way to add classes to the body_class
function based on the current page?
For example if you're on the homepage, as well as the usual generated classes, the <body>
element would also have front
and main-page
classes. Then if you were on the "About" page it would have inner-page
and sixcol
classes instead.
I (unsuccessfully) tried this;
function body_class_variants($variants) {
if (is_home() || is_front_page()) {
$variants = 'front main-page';
} else {
$variants = 'inner-page sixcol';
}
return $variants;
add_filter('body_class','body_class_variants');
which was based off this function that I use to output the page name into the body class
function body_class_names($classes) {
global $post;
$name = $post->post_name;
$classes[] = $name;
return $classes;
}
add_filter('body_class','body_class_names');
But to no avail.
Any help would be greatly appreciated.
<?php body_class(); ?>
function is added to the body tag? It should look like<body <?php body_class(); ?>>
. See codex.wordpress.org/Function_Reference/body_classbody_class_variants
function is failing? It looks to me like you are overwriting and not returning the existing variants, rather than adding to the array as you do inbody_class_names