strangeQuirks
strangeQuirks

Reputation: 5910

Wordpress: Add dynamic class to body based on what page is displayed

I need to add a class to specific pages that are under the following route: www.mydomain.com/special/...

So whenever /special/ is in the current url, i thought I could add a class to the theme header.php file:

<body id="landing-page" <?php body_class(); ?>>

Would this be the way to go? How could I get the current page url?

Upvotes: 0

Views: 2213

Answers (1)

lakshman rajput
lakshman rajput

Reputation: 507

You can add below snippet to your functions.php

add_filter( 'body_class', 'custom_class' );
function custom_class( $classes ) {
    if ( is_page('special') ) {
        $classes[] = 'myclass';
    }
    return $classes;
}

Or

add_filter( 'body_class', 'custom_class' );
    function custom_class( $classes ) {
        if(strpos($_SERVER['REQUEST_URI'], "special") !== false){
            $classes[] = 'myclass';
        }
        return $classes;
    }

Upvotes: 3

Related Questions