How to add a body class to all interior pages except the homepage

I want to add a class to the body tag to all pages EXCEPT the homepage. Right now I have.

<?php body_class('interior'); ?>

Read More

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?

Related posts

Leave a Reply

3 comments

  1. Try it:

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

    Or this:

    <?php
    body_class( ! is_home() ? "interior" : "" );
    ?>
    
  2. 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;
    }
    
  3. 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;
    }