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

Related posts

Leave a Reply

1 comment

  1. 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;
    }