Add Useful Body Classes
Adds the post type and slug of the current page plus a logged-in or logged-out class to the body tag, for precise CSS targeting.
Opens WPIDE on
Code
add_filter( 'body_class', function ( $classes ) {
if ( is_singular() ) {
$post = get_queried_object();
if ( $post instanceof WP_Post ) {
$classes[] = sanitize_html_class( $post->post_type . '-' . $post->post_name );
}
}
$classes[] = is_user_logged_in() ? 'is-logged-in' : 'is-logged-out';
return $classes;
} );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.