cboy
cboy

Reputation: 179

Conditional body class for home page

I am trying to add a conditional body class that will only be on the home page. I found a conditional filter for woocommerce shop page, but when I change is_shop to is_home, it does not work. Here is what I am trying to modify for "home" page.

add_filter( 'body_class','my_body_classes' );
function my_body_classes( $classes ) {

if ( is_home() ) {

    $classes[] = 'class-name';
    $classes[] = 'class-name-two';

}

return $classes;

}

Upvotes: 0

Views: 1442

Answers (2)

Nathan Dawson
Nathan Dawson

Reputation: 19308

The conditional tag you were using, is_home(), rather misleadingly only applies to the 'blog homepage'.

The 'blog homepage' isn't necessarily your site's homepage. It's dependent upon the setting used for "Front page displays" under Settings -> Reading. If your homepage is set to a static page then is_home() will return true on the "Posts" page.

The conditional you're looking for is is_front_page(). This function is going to return true when on the homepage, regardless of whether it's a blog index or a static.

Docs:

Upvotes: 2

cboy
cboy

Reputation: 179

I added "home" after is_page and it's working now.

add_filter( 'body_class','my_body_classes' );
function my_body_classes( $classes ) {

if ( is_page('home') ) {

    $classes[] = 'home-page';
    $classes[] = 'class-name-two';

}

return $classes;

}

Upvotes: 0

Related Questions