Created
April 2, 2021 05:51
-
-
Save leek/763023f26720be9a68bc09e6ae47298c to your computer and use it in GitHub Desktop.
Add current page slug to <body> class in WordPress.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Alternative: | |
// Install https://github.com/roots/soil | |
// Add slug to <body> class | |
add_filter('body_class', function ($classes) { | |
// Add post/page slug if not present | |
if (is_single() || is_page() && !is_front_page()) { | |
if (!in_array($slug = basename(get_permalink()), $classes)) { | |
$classes[] = "page-{$slug}"; | |
} | |
} | |
return $classes; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment