How do I add a body class to specific pages?

Here is what I have so far in my functions.php.

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 classes as they are. 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. add_filter('body_class','cp_new_body_classes');
    function cp_new_body_classes($classes) {
        if( !is_page_template() )
            $classes[] = 'reg-page';
    
        return $classes;
    }
    

    The problem was that you are only returning the classes if !is_page_template(), but you want to return the classes always as it contains the other classes you didn’t add yourself in an array, you just want to add some then return them like normal.