Вопрос

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?

Это было полезно?

Решение

Try 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;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с wordpress.stackexchange
scroll top