php - Handling Body class based on Template

admin2025-06-05  3

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?

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?

Share Improve this question edited Dec 24, 2018 at 8:56 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Dec 24, 2018 at 6:56 Richa SharmaRicha Sharma 497 bronze badges 2
  • 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 Commented Dec 24, 2018 at 6:58
  • please check url : code.tutsplus/tutorials/… – vikrant zilpe Commented Dec 24, 2018 at 6:59
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749071796a316111.html

最新回复(0)