There are lots of cases that you would like to add custom CSS only applies if the page is not the home page. As most of the devs know, WordPress is adding custom classes to body for each page like the id of the page mostly something looks like “page-1234“. This is useful for page specific cases. From time to time WordPress themes don’t behave nicely as it seems on the demo and especially for the sub-pages. Since there is no custom body class for the not-home we have to do it manually by adding a function to functions.php file of the theme.
There is a WordPress function called is_front_page() which is checking whether if you are on the home page or not. In most cases, this is useful. Some themes may not like it but works in most cases. So, if you add the following code to your function.php file it will add a “not-home” class to your body.
//add not-home to body classes
function add_not_home_body_class($classes) {
//add not-home to body classes if it is not on the home
if( !is_front_page() ) $classes[] = 'not-home';
//send the classes back
return $classes;
}
//filter the body classes with our new function
add_filter('body_class','add_not_home_body_class');
This will work perfectly fine in most cases but if doesn’t work -which is rare in my experience, you can use is_page() function to target your home page slug so it will do the same thing. Also, as an another use for this case specific sitiuayion, you may want to assign a class every page except for, let’s say blog, and you can use this selector for CSS, JavaScript or any other purposes.
function add_not_home_body_class($classes) {
if( !is_page('home-new') ) $classes[] = 'not-home';
return $classes;
}
add_filter('body_class','add_not_home_body_class');
As you can see, the code above assumes that the page you are targeting is “home-new“. You can change it any slug that you want.