Creating Unique Styles for WordPress Pages

Saturday, March 6th, 2010 Tags: ,

In WordPress 2.8, there is one small but very useful feature, both for WP web developers, and for bloggers. This is an opportunity to change the appearance of any individual page or group of pages without the need to write either the functions/conditions on php or install plugins. All you need to do is simply add your desired style to your css file.

How does it work?

In WordPress 2.8 has been added to the function body_class(). It works like so

<body <?php body_class(); ?>>

So, we just need to apply such function to the body.

If your theme is adapted for WordPress 2.8, this feature is probably already present in the code. In case you can’t find that function, it's enough to search for

<body>

tag in header.php file of your theme and replace it with

<body <?php body_class(); ?>>

Usage

What do we get from using this function? It’s very simply; we get a <body&rt; tag with additional classes, such as:
<body class="home blog"&rt;
or
<body class=" single postid-1001 parent-page-id-0 page-template-default"&rt;

What does this give us? Let’s look at some examples.

Adding to your website’s css file:

.postid-3 #respond{

display: none; 

}

Will kill comments form from post with id=3, just leaving comments.


.home{

background: #f00;

}

Only make the background of the home page in red.


.page h2{

font-size: 30px;

}

Increases the size of the header only on the pages (not blog entries) of the site.

Anyway, these are just simple examples. Using this feature you can make almost any changes in the CSS file for a single page or page group.

Documentation

Below you can see a full list of body classes that can be used for custom styling (just apply desired styles to them). That long list lets us know that style can be applied to almost any page, even to the third page of a search results, even to the tag, even to the author.

  • rtl
  • home
  • blog
  • archive
  • date
  • search
  • paged
  • attachment
  • error404
  • single postid-(id)
  • attachmentid-(id)
  • attachment-(mime-type)
  • author
  • author-(name)
  • category
  • category-(name)
  • tag
  • tag-(name)
  • page
  • page-parent
  • page-child parent-pageid-(id)
  • page-template page-template-(template file name)
  • search-results
  • search-no-results
  • logged-in
  • paged-(page number)
  • single-paged-(page number)
  • page-paged-(page number)
  • category-paged-(page number)
  • tag-paged-(page number)
  • date-paged-(page number)
  • author-paged-(page number)
  • search-paged-(page number)

For WordPress huge fans and those who wants more, there is an opportunity to add their custom classes to body_class via a standard WordPress way:

<?php
add_filter('body_class','my_body_classes');
function my_body_classes($classes, $class) {
	// add 'my-class' to the $classes array
	$classes[] = 'my-class';
	// return the $classes array
	return $classes;
}
?>

There is no official documentation for body_class() at the moment, but as usually happens in WordPress, the function is made very simply and wisely. So, it is not difficult to understand it. A little more details can be found in the body_class() function’s code.

Thank you and good luck! :)

Tags: ,

If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!

ABOUT THIS AUTHOR

My name is Nick Plekhanov and I am a 22 year old passionate freelance web developer, user interface designer, and blogger located in the small russian town. I just love building awesome web apps, and cool looking, modern websites in my spare time and following what's new in the web world. My own web development blog is coming soon. You can get in contact with me by following me on twitter.
  1. March 7, 2010 at 3:55 am
  2. hans_hansen
    March 11, 2010 at 2:17 pm
  3. May 7, 2010 at 6:41 am

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Popular Series

Output Buffering Articles
Build a Custom AJAX and PHP Contact Form
The Ultimate Image Gallery Manager.
ThemeForest Premium Site and WordPress Templates