Add a Different CSS Body Class to a Page in WordPress

Using WordPress filter ‘body_class‘ you can add a unique CSS class to the body element of a page by passing in what pages you want the new CSS class applied to.

<?php
add_filter( 'body_class', 'extra_body_class' );
// Add specific CSS class by filter
function extra_body_class( $classes ) {
if ( is_single( 'blank-title-and-content-page-template-in-genesis-theme' )) // Set conditionals - example is using a slug
$classes[] = 'bare-bones';
return $classes;
}

So in the above code I want an additional class ‘beaverbuilder’ applied just to the page with the ID of 8. This code will add the extra CSS class to the other default classes just for that page and return the default classes for all the other posts and pages.

To target other posts/pages/archives etc set your desired conditionals.

 

1 Comment

  1. Nedroof on December 13, 2016 at 7:24 pm

    It’s not working for me.

Leave a Comment





%d bloggers like this: