Make use of the body_class function

The function body_class was introduced in WordPress 2.8 and should be included in every theme.

Include it in your HTML like so:

<body <?php body_class(); ?>

And you will end up with something like this

<body class="page page-id-4 page-parent page-template page-template-default">

It can come quite handy when you want pages to have different color themes for example. Just like the classes in your navigation, the classes can be altered with the body_class filter.

For example, filter the classes so that page-id-4 becomes page-YOUR-SLUG

function my_theme_body_class($classes, $class){
	global $post;
	foreach($classes as &$str){
		if(strpos($str, "page-id-") > -1){
			$str = "page-" . $post->post_name;
		}
	}
	return $classes;
}
add_filter("body_class", "my_theme_body_class", 10, 2);

Hope you find it useful!

Advertisements
About

Web Developer @ Oakwood Creative - www.oakwood.se

Tagged with: ,
Posted in Beginner, Best Practices

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

About

WordPress Quick Tips is a blog supplying great tips about WordPress.

We hope to create a great knowledge resource for WordPress developers as well as serving a reminder for all the forgetful ones.

The blog is created and run by Vincent of Oakwood Creative

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 120 other followers

%d bloggers like this: