开发者

Adding a body class

add_filter('body_class','cp_new_body_classes');
function cp_new_body_classes($classes) {
    if( !is_page_template() ) {
        $classes[] = 'reg-page';
开发者_Go百科    return $classes;
    }
}

I'm trying to append the class of reg-page to pages that are not page templates. For the pages that are page templates, I just want to leave the default WordPress classes. When trying the code above, the class of reg-page gets added like I want to pages which are not page templates but the other pages (which are page templates) get left with no classes at all. How can I fix this code?


Currently, if the given URL does not correspond to a page, nothing is returned by the function. WordPress is handing you the classes that it has already evaluated and, by returning null, you tell it to forget about all of those. To fix: return $classes even if the condition fails.

add_filter('body_class','cp_new_body_classes');
function cp_new_body_classes($classes) {
    if( !is_page_template() )
        $classes[] = 'reg-page';

    return $classes;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜