Skip to content

Instantly share code, notes, and snippets.

@donini
Last active September 18, 2019 14:11
Show Gist options
  • Save donini/04c261a8c8337197277a499c7598a388 to your computer and use it in GitHub Desktop.
Save donini/04c261a8c8337197277a499c7598a388 to your computer and use it in GitHub Desktop.
Add Custom Class To Your Body Element
<?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