Wordpress Technology

Wordpress Tips, Tutorials, Hacks

Add Post Name to Body Class

Sometimes doing the css layout, we need to assign the specific class name to the body tag. To achieve the task, the only thing you have to do is to copy the following code 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' );

If enjoy the tip please consider sharing it!


Both comments and pings are currently closed.

© 2009 | Made by Vancouver Web Design | Internet Marketing