Site icon Experience Wordpress Developer Online – Design Develop and Maintenance Support

Add custom body class based on page title in wordpress

The body class in WordPress is a class or series of classes that are applied to the HTML body element. This is useful for applying unique styles to different areas of a WordPress site as body classes can be added conditionally. Let’s see how to Add custom body class based on page title in wordpress. So Here is my code, Copy it and Add this code in your theme’s functions.php file of your active child theme (or theme).

After that go to Your website on browser and open any page of your website and see your body class added based on your page title. Here i open about-us page on my website. see my about class added in body.

add_filter( 'body_class','my_body_classes' );
function my_body_classes( $classes ) {
    global $post;
    $name = $post->post_name;
    $classes[] = $name;
     
    return $classes;
     
}
Exit mobile version