I want to add a class to the body tag to all pages EXCEPT the homepage. Right now I have.
<?php body_class('interior'); ?>
But it adds 'interior' to ALL pages including the home page.
What is the best standard way of adding a class to the body tag?
I want to add a class to the body tag to all pages EXCEPT the homepage. Right now I have.
<?php body_class('interior'); ?>
But it adds 'interior' to ALL pages including the home page.
What is the best standard way of adding a class to the body tag?
Share Improve this question edited May 11, 2013 at 22:14 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Feb 13, 2013 at 19:41 breezybreezy 1352 silver badges7 bronze badges3 Answers
Reset to default 5Try it:
<?php
$class = ! is_home() ? "interior" : "";
body_class( $class );
?>
Or this:
<?php
body_class( ! is_home() ? "interior" : "" );
?>
The filter body_class
can be used for this.
add_filter( 'body_class', 'body_class_wpse_85793', 10, 2 );
function body_class_wpse_85793( $classes, $class )
{
// This one overrides all original classes
if( is_home() )
$classes = array( 'interior' );
// This one simply adds a new class
//if( is_home() )
// $classes[] = 'interior';
return $classes;
}
Please use the body_class
filter to add any class to the body tag in WordPress.
Add the below code to your functions.php file.
Add class to the body tag for all the pages.
add_filter('body_class', 'wp_body_class');
function wp_body_class($class)
{
$class[] = 'my-custom-class'; // Add your class name here. Multiple class names can be added 'classname1 classname2'.
return $class;
}
Exclude Home Page
if (!is_front_page() ) { // exclude home or any other page.
add_filter('body_class', 'wp_body_class');
}
function wp_body_class($class)
{
$class[] = 'my-custom-class'; // Add your class name here. Multiple class names can be added 'classname1 classname2'.
return $class;
}