Skip to content

Add post/page name to body class

The following snippet adds a class in the following format to the body element

{post_type}-{post_name}

For example

post-hello-world
page-about-me

Add this to your plugin or theme’s functions.php

add_filter( 'body_class', 'wpprogrammer_post_name_in_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;
}

Categories: .

Comment Feed

No Responses (yet)



Some HTML is OK

or, reply to this post via trackback.