StepanP
StepanP

Reputation: 33

How to add a class to body element if it's a frontpage in WordPress?

last days I have been writing my own WordPress theme, but I run into another problem. These times I have no clue, how to make it possible.

I would like to add a class to every frontpage on my website. So if a single page becomes a frontpage, it will get another class to body tag like "home".

Almost every premium theme gots this funcion, but I just cant find the solution.

Does anybody have any idea?

Thank you! Stepan

Upvotes: 1

Views: 2967

Answers (1)

Sami Ahmed Siddiqui
Sami Ahmed Siddiqui

Reputation: 2386

You can add the class in body tag using body_class filter as shown below:

function home_body_class($classes) {
    if ( is_front_page() ) {
        $classes[] = 'home';
    }

    return $classes;
}

add_filter( 'body_class', 'home_body_class' );

You can manipulate the condition for the static homepage, blog page and so on.

Upvotes: 5

Related Questions