How to Add Post Slug to Body Class in WordPress

Adding the following code to your functions.php file will allows you to add post slug as a CSS class to body element in WordPress. We are using body_class filter to do this task.

Quick Tip: Displaying Theme Information in WordPress

//Post Slug Body Class
function add_post_slug_body_class( $classes ) {
    global $post;
    if ( isset( $post ) ) {
        $classes[] = $post->post_name;
    }
    return $classes;
}
add_filter( 'body_class', 'add_post_slug_body_class' );

 

0 Comments

Leave a Comment.