0
add_filter( 'body_class', 'custom_body_class' );

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

Above code works fine to add a class in a page which id 38034. Now I want to add same class in multiple pages, how I can do that?

can I write if ( is_page( '38034, 10883, 12031' ) ) {

Note: I have no php knowledge.

3

Note that you must always return the classes in your callback.

So move this part:

return $classes; 

out of the if condition.

The is_page() does support multiple ID's when it's in an array.

This should work:

add_filter( 'body_class', 'custom_body_class' );

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

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.