1

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?

4

Try it:

<?php
$class = ! is_home() ? "interior" : "";
body_class( $class );
?>

Or this:

<?php
body_class( ! is_home() ? "interior" : "" );
?>
  • Thanks for your answer! however it still adds interior to the 'home' page as well. – breezy Feb 13 '13 at 19:52
  • Oddly, worked here. – Thiago Locks Feb 13 '13 at 20:03
  • 1
    Try it: <body <?php body_class( ! is_home() && ! is_front_page() ? "interior" : "" ); ?>> – Thiago Locks Feb 13 '13 at 20:04
  • I adjusted my reading settings to static and have no pages selected & it worked. – breezy Feb 13 '13 at 20:06
1

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;
}
  • very nice. One more reason not to copy header.php down into my child template directory if I don't need to make additional modifications to the file. – klewis Oct 16 '17 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.