If you’ve done much theme development, you’ll know how handy it is to be able to add specific classes to the <body> tag depending on which page is being viewed. Since WordPress version 2.8, that’s a simple thing to do. That version added a function called ‘body_class()‘ which, used in conjunction with the correct action, lets us easily add new classes whenever we need them, with a few lines in the theme’s functions.php file.

One of the biggest advantages to using this function/action method is that it keeps the logic out of the header.php template file, giving us cleaner coding in the template, and allowing us to split out appearance/function even more.

Using the body_class() function

Using the body_class() function is easy – write a function to add a class or classes depending on certain conditions, then ‘hook’ that function into the body_class action. (As always, a quick look at the WordPress Codex entry for body_class() can be helpful.)

One note – your theme must be coded to handle the body class function. Check the theme’s “header.php” file, look for the <body> tag. It needs to look like this:

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

(i.e., the tag has the call to the body_class() function). If it isn’t there, add it.

## add a custom body class
add_action( 'body_class', 'ilwp_add_my_bodyclass');
function ilwp_add_my_bodyclass( $classes ) {
  $classes[] = 'my-custom-class';
  return $classes;
}

Just adding a class like that wouldn’t do us a lot of good, though. We need some conditions, which we’ll add using WordPress conditional tags.

Adding a class to a specific page by page slug:

## add a custom body class
add_action( 'body_class', 'ilwp_add_my_bodyclass');
function ilwp_add_my_bodyclass( $classes ) {
  if ( is_page( 'sample' ))
    $classes[] = 'my-custom-class';
  return $classes;
}

Adding a class to a specific page by page ID:

## add a custom body class
add_action( 'body_class', 'ilwp_add_my_bodyclass');
function ilwp_add_my_bodyclass( $classes ) {
  if ( is_page( '1' ))
    $classes[] = 'my-custom-class';
  return $classes;
}

Adding a class to a specific category page, by category slug:

## add a custom body class
add_action( 'body_class', 'ilwp_add_my_bodyclass');
function ilwp_add_my_bodyclass( $classes ) {
  if ( is_category( 'custom-category' ))
    $classes[] = 'my-custom-class';
  return $classes;
}

Or, we can get a little more complicated…

Adding a class to specific post, but only if that post is a ‘video’ post (assuming you are using custom post types):

## add a custom body class
add_action( 'body_class', 'ilwp_add_my_bodyclass');
function ilwp_add_my_bodyclass( $classes ) {
  global $post;
  if ( 'video' == $post->post_type AND 'my-post' == $post->post_name )
    $classes[] = 'my-custom-class';
  return $classes;
}

Do you need to drill down to a very specific page/post?

## add a custom body class
add_action( 'body_class', 'ilwp_add_my_bodyclass');
function ilwp_add_my_bodyclass( $classes ) {
  global $post;
  $classes[] = $post->post_name;
  return $classes;
}

You’re really only limited by your imagination when it comes to adding custom post types. You can use any of the WordPress Conditional tags, you have access to the $post object for specific properties – the sky is the limit.

{ 0 comments }

We’ve MOVED

If you are at all observant, you’ll have noticed that the URL to ILikeWordPress.com has changed to ILikeWP.com. This is because of a very polite email from Otto at wordpress.org informing me that Automattic is starting to aggressively protect the WordPress trademark. I’ve put in place automatic redirects, but I’d ask that you update any [...]

Read the full article →

How to Display Your Post/Page Content in Two Columns

Ok, so life has intruded and I haven’t updated things here as often as I’d hoped. So here’s a little treat: how to display your content in two columns. Why should you display content in two columns? Narrower text columns can increase readability, especially if you have a wide ( 600px or more ) content [...]

Read the full article →

Simple Link Cloaker Plugin Available for Download

Because some days are better than others, I managed to upload the wrong zip file of the link cloaker plugin. That error has been corrected. This is the correct download link for the Simple Link Cloaker plugin. It seems that if I had named the Simple Link Cloaker plugin The Fabulous Redirector instead, all would [...]

Read the full article →

The Case Of the Missing Post

A client came to me with a strange problem. He’d written and published a post, but when he tried to view it on his site he got a 404-Not Found error. Usually, when there is a problem with posts or pages disappearing the culprit is permalink-related. Most of the time, a simple click of the [...]

Read the full article →

You Do Not Have Sufficient Permissions to Access This Page

More than a few people have contacted me after seeing this message. It usually appears after upgrading to WordPress version 3 or above, when accessing a plugin’s Dashboard menu. This article isn’t an exhaustive treatment of this issue, but if you’re a developer you’ll at least walk away with an idea of what needs to [...]

Read the full article →

Fixing the Blank Screen Syndrome on a WordPress Blog

With the advent of WordPress version 3, my most common fixit request has been, “Help! I just see a white screen on my blog!” I’ll share with you what I’ve found to be the most common cause, but first there’re a few things you can do to at least get back up and running again. [...]

Read the full article →

Changing WordPress Auto-Save and Revisions Settings

The WordPress  team did a great and wonderful thing when they included auto-save and revisions for posts/pages. If you’re in a collaborative environment, referring to previous revisions can be a useful tool. And auto-save? If you haven’t had a computer crash in the middle of writing a blog post, you’re probably in the minority On [...]

Read the full article →