Skip to content

Instantly share code, notes, and snippets.

@vades
Last active May 21, 2019 06:42
Show Gist options
  • Save vades/9d78a2743d5f2af924b50153a841e954 to your computer and use it in GitHub Desktop.
Save vades/9d78a2743d5f2af924b50153a841e954 to your computer and use it in GitHub Desktop.
Add page slug to body class in WordPress

Add page slug to body class

function wps_add_slug_to_body($classes)
{
    global $post;
    if (is_home()) {
        $key = array_search('blog', $classes);
        if ($key > -1) {
            unset($classes[$key]);
        }
    } elseif (is_page()) {
        $classes[] = sanitize_html_class($post->post_name);
    } elseif (is_singular()) {
        $classes[] = sanitize_html_class($post->post_name);
    }

    return $classes;
}

Usage

add_filter('body_class', ' wps_add_slug_to_body');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment