
Introduced in WordPress 2.8, body_class() is a very useful function when it comes to styling themes. In this recipe, I’ll show you how you can add post name to the function for an even easier styling.

Introduced in WordPress 2.8, body_class() is a very useful function when it comes to styling themes. In this recipe, I’ll show you how you can add post name to the function for an even easier styling.
The only thing you have to do is to copy the following function and paste it on your theme functions.php file. Once saved, the post/page name will be automatically be added to the body class.
function wpprogrammer_post_name_in_body_class( $classes ){
if( is_singular() )
{
global $post;
array_push( $classes, "{$post->post_type}-{$post->post_name}" );
}
return $classes;
}
add_filter( 'body_class', 'wpprogrammer_post_name_in_body_class' );
Thanks to Utkarsh Kukreti for this great hack!
Leave a Comment