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!

Leave a trackback from your own site.

One Response to “Add Post Name to Body Class”

  1. Hi,

    Thanks for the tip! Any tips how to add the post author name to the body class as well?

    Thanks,
    Thomas.

Leave a Reply

You must be logged in to post a comment.

© 2009 | Made by Vancouver Web Design | Internet Marketing