简体   繁体   中英

How to add a custom body class for pages that are not the front page?

I am trying to add a custom body class .custom-body in a WordPress site to all pages that are NOT the front page:

To my functions.php I added the following filter:

if ( !is_front_page() ) {
    add_filter('body_class','my_class_names');
    function my_class_names($classes) {
        // add 'class-name' to the $classes array
        $classes[] = 'custom-body';
        // return the $classes array
        return $classes;
    }
}   

In my header I call the body-function:

<body <?php body_class(); ?>>

The problem is that custom-body is added to all pages, ie the if statement doesn't seem to work.

Any idea how to get this working?

Adding the CSS-class conditionally instead of the filter solved the problem:

function my_class_names($classes) {
    // add 'class-name' to the $classes array
    if( !is_front_page() ) $classes[] = 'custom-body';
    // return the $classes array
    return $classes;
}
add_filter('body_class','my_class_names');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM