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

In header.php I have written the above code.

But I want when this page template is in use:

page-customer.php

then

body_customer

class should go above. Is this possible through some hook?

3

there is a filter for that and it's called... body_class ;) This should work for your case:

function add_my_custom_body_class( $classes ) {
    if ( is_page_template( 'page-customer.php' ) ) {
        $classes[] = 'body_customer';
    }

    return $classes;
}
add_filter( 'body_class', 'add_my_custom_body_class', 10, 1 );

if you wanna add class based on page ID (as you've mentioned in comments), you can change the logic inside the function and add class only when particular page is queried, let's say your page ID is 3:

if ( 3 === get_the_ID() ){
     $classes[] = 'your_additional_class';
}
| improve this answer | |
  • Thanks I was interested in adding based on page template only. Thanks for the help. – The WP Intermediate Sep 2 '18 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.