1

Through a PHP function, I think I can handle body class, but I think WordPress would have some specific way to handle it.

If Home.php then class in the body should be wbody else it should be bgody.

As I said I can write PHP functions to print class based on the template, but Is there a more precise way to do this in the case of WordPress?

  • please try this code : add_filter( 'body_class','halfhalf_body_class' ); function halfhalf_body_class( $classes ) { if ( is_page_template( 'page-halfhalf.php' ) ) { $classes[] = 'halfhalf-page'; } return $classes; } – vikrant zilpe Dec 24 '18 at 6:58
  • please check url : code.tutsplus.com/tutorials/… – vikrant zilpe Dec 24 '18 at 6:59
1

I'm not sure if I understand your question correctly, but...

If you want to set body classes based on current page, then you can use this code

function my_body_class( $classes ) {
    if ( is_home() ) {
        $classes[] = 'wbody';
    } else {
        $classes[] = 'gbody';
    }
    return $classes;
}
add_filter( 'body_class', 'my_body_class' );

Of course you can use other conditions in there and the list of Conditional Tags might come in handy to.

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.