I’ve created a function to add a class to the body, however if should only apply to a single page ID. This normally wouldn’t be a problem, but it is breaking the layout for other pages.
Filter Function
add_filter('body_class','add_blog_to_body_class');
function add_blog_to_body_class($classes) {
$classes[] = 'blog';
return $classes;
}
My Condition (that isn’t working)
function add_blog_to_body_class($classes) {
$classes[] = 'blog';
if ( is_page('ID') ) {
return $classes;
}
}
I’ve wrapped the entire function, the declaration, the add_filter
hook, and everything without luck.
How can I have this function work on a single page ID?
1 Answer
It’s important to always return the value in filter callbacks, otherwise it’s like adding the __return_null()
callback.
Here’s an example that adds the blog
class only to page with ID 40:
add_filter( 'body_class', 'add_blog_to_body_class' );
function add_blog_to_body_class( $classes )
{
if ( is_page( 40 ) )
$classes[] = 'blog';
return $classes;
}