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
    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 '19 at 22:32
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.

| improve this answer | |

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.