Aweber - Email Marketing Made Easy
Thesis Custom Body Class – How To Use It To Create Targeted Custom Styling Photo by: Richard Fisher

Thesis Custom Body Class – How To Use It To Create Targeted Custom Styling

September 6, 2010 · 2 comments

in Thesis

There are some situations where you might want to target a specific page, post or group of pages in Thesis and apply unique custom styling to those posts or pages. The Thesis custom body class is the best way to handle this.

Before I get into how to use this, I wanted to take a quick moment and explain a little about custom classes. When you make design customizations to Thesis, it’s done using the custom.css file. The way Thesis knows to use this file is in the “Site Options” where you can enable or disable this. When it’s enabled, Thesis adds a css class called ‘custom’ to the body tag. This way, when you create your css customizations, you add .custom to each line of your css code.

The same concept applies to Thesis custom body classes. You have the ability to assign an additional custom body class to your posts or pages, and when you do this, you are now able to specifically target that page or post by replacing .custom with your custom body class. If you’d like to read a little more about why this works, check out this post on css specificity over at the DIY Themes blog.

How to Use Thesis Custom Body Classes

There are two ways to assign a custom body class to your posts or pages. The first way is to simply use the custom body class field which you will find in the post/page editing screen. It looks like this:

Thesis Custom Body Class

All you do is enter the name of your body class in the space provided and you’re done. Thesis will now add this custom body class to the body tag of the page or post in question.

I recently had to use this method to target a couple of pages on a site I was building for a client. I needed to add a different background image to each of the pages main content sections and the easiest way to handle this was to add a custom class to each of the pages, then target those pages in the custom.css file.

Thesis Custom Body Classes Filter

If you have a group of pages you want to target, then this first method isn’t recommended because it can become tedious adding these classes in manually to every page. For this I’d recommend the second method which is the thesis_body_classes filter. Don’t let this scare you, but it looks like this:

function custom_body_class($classes) {
	$classes[] = 'your_custom_class';
	return $classes;
}
add_filter('thesis_body_classes', 'custom_body_class');

This basically does 4 key things:

  1. Creates a function called custom_body_class and passes the $classes parameter so it can be defined
  2. Defines $classes as the name of the new custom class (your_custom_class)
  3. Returns $classes so that the class will actually show up in the HTML
  4. We then add this function to the thesis_body_classes filter

Now that you have the basic format down, you probably realize that by doing this, it will add the custom class to every page and post across the entire site. So we need to apply a condition to the function by using a WordPress conditional tag. Let’s say you wanted to add the custom class to the home page only, then use this code:

function custom_body_class($classes) {
	if(is_home()) {
		$classes[] = 'your_custom_class';
		return $classes;
	}
}
add_filter('thesis_body_classes', 'custom_body_class');

Now you can substitute the is_home() part for a different conditional tag. If you wanted the custom body class applied to all category pages, then use is_category(). All single post pages, will be is_single() and so on. You get the idea.

now add custom styles by targeting the thesis custom body class in your custom stylesheet

Now that you’ve created your custom body classes, you will want to get to the reason for why you’re doing this in the first place. That’s to target these posts and/or pages and apply custom styling to them. You do this of course inside of your custom.css file. Just replace .custom with your new class. Here’s just one example out of many for how this would look:

.your_custom_class #content { background-color:#EFEFEF; }

And that’s about it. Pretty simple. There’s some advanced things you can do with this, I’d recommend you take a look at the advanced examples in this post by Adam Baird over on the artofblog.com site.

Was this post helpful? Consider subscribing to my blog via RSS or

This Site Runs on the Thesis WordPress Theme

Thesis Theme thumbnail

If you're someone who doesn't understand a lot of PHP, HTML, or CSS, Thesis will give you a ton of functionality without having to alter any code. For the advanced user, Thesis has incredible customization possibilities via extensive hooks and filters. And with so many design options, you can use the template over and over and never have it look like the same site.

If you're more familiar with how websites work, you can use the fantastic Thesis User's Guide and world-class support forums to make more professional customizations than you ever thought possible. The theme is not only highly customizable, but it allows me to build sites with a much more targeted focus on monetization than ever before. You can find out more about Thesis below:

{ 1 comment… read it below or add one }

corona del mar braces September 25, 2010 at 6:17 am

Awesome post paul, it’s been a while since I’ve been on here. I see that nobody has lost their passion. Good to be back.

Reply

Leave a Comment

When commenting, you can use basic HTML tags. If you are pasting in any code, please escape it here and then include that code within <pre></pre> tags. Thanks!

CommentLuv badge

{ 1 trackback }

Previous post:

Next post: