简体   繁体   English

如何为非首页的页面添加自定义正文类?

[英]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: 我正在尝试在WordPress网站.custom-body自定义主体类.custom-body添加到非首页的所有页面中:

To my functions.php I added the following filter: 在我的functions.php ,添加了以下过滤器:

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函数:

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

The problem is that custom-body is added to all pages, ie the if statement doesn't seem to work. 问题在于custom-body已添加到所有页面,即if语句似乎不起作用。

Any idea how to get this working? 任何想法如何使它工作?

Adding the CSS-class conditionally instead of the filter solved the problem: 有条件地添加CSS类而不是过滤器可以解决此问题:

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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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