Add page name to body class in WordPress

Published on July 18, 2019 at 5:37 pm by reMarkable websites

Need to add styles to a specific page on your website? Add the following code to functions.php:-

// Add page slug to body class
function add_slug_to_body_class($classes)
    global $post;
    if ( isset( $post ) ) {
    $classes[] = $post->post_name;
    return $classes;

Now you can target individual pages in your CSS. For example, to apply styles to the about page use this:-

body.about { [styles go here] }


Comments are closed here.

Back To Blog