Last active
September 18, 2019 14:11
-
-
Save donini/04c261a8c8337197277a499c7598a388 to your computer and use it in GitHub Desktop.
Add Custom Class To Your Body Element
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 | |
add_filter('body_class', 'add_slug_to_body_class'); | |
public function add_slug_to_body_class( $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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment