Automatically add post name to the body class-WordPress hack

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' );

source:wprecipes

Web Hosting

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

*