0

been hacking at this for some time and haven't been able to make much progress. The goal is to add a custom body class to a specific page. -I know there are a few answered posts on this topic, and while the solution provided works if I add the custom body class globally, it fails when I add the if statement. Any ideas of what might prevent this from registering the custom body class?

These Fail:

add_filter('body_class', 'custom_body_class');
function custom_body_class($classes) {
    if (is_page('shop'))
        $classes[] = 'services';
    return $classes;
}

Or

add_filter('body_class', 'custom_body_class');
function custom_body_class($classes) {
    if (is_page('4'))
        $classes[] = 'services';
    return $classes;
}

but this works, unfortunately globally:

add_filter('body_class', 'custom_body_class');
function custom_body_class($classes) {
  //  if (is_page('shop'))
        $classes[] = 'services';
    return $classes;
}

I am on Genesis Framework, WPEngine, have WooCommerce and all are up to date. -Weird.

1
  • 1
    I don't know if you are working with WooCommerce but in case I would suggest to use is_shop() as condition.
    – guido
    Jun 1, 2019 at 22:32

2 Answers 2

0

you could try to use this:

add_filter('body_class', 'custom_body_class');
function custom_body_class($classes) {
    global $post;
    if ($post->ID == 346) {
        $classes[] = 'services';
    }
    return $classes;
}

This can be used anywhere as its pulling the global variable first.

0

This will works! Fine

add_filter('body_class','shop_body_classes');
function shop_body_classes($classes){
    if (is_shop()){   
        $classes[] = 'class-name-1';
        $classes[] = 'class-name-2';    
    } 
    return $classes;  
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.