How do I add a custom body class for a specific page ID?

I have added this code to my child theme's functions.php file and use the new class but it styles the entire site, not the page I added to the code. What am I doing wrong?

add_filter('body_class','custom_body_class');

function custom_body_class($classes) {
    if( is_page('38034') ) {
        $classes[] = 'new-class';
        return $classes;
    }
}

Topic post-class Wordpress

Category Web


The ID can/should be given without quotes (otherwise if you a page with '38034' as slug/post_name, this will be used instead of the page with the ID 38034). And you want to return $classes no matter if you added your own or not.

add_filter('body_class', 'custom_body_class');
function custom_body_class($classes) {
    if (is_page(38034))
        $classes[] = 'new-class';
    return $classes;
}

Your function name does not match. Try this..

add_filter( 'body_class', 'custom_body_class' );

function custom_body_class( $classes ) {
    if ( is_page( '38034' ) ) {
        $classes[] = 'new-class';
        return $classes;
    }
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.