最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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 badges
Add a comment  | 

3 Answers 3

Reset to default 5

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;
}
发布评论

评论列表(0)

  1. 暂无评论