$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Handling Body class based on Template|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Handling Body class based on Template

matteradmin9PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far