We all have seen WordPress sites with repeating backgrounds that span beautifully across the entire page. This property is usually applied in the body tag of your template. What about those portfolio websites that use a different background image in the front page? Or those magazine styled websites that apply different backgrounds for their category pages? How’d they do that?
The problem
For anyone who’s developed a WordPress theme would know how to break it up in templates: header.php, sidebar.php , footer.php etc. And as I’ve mentioned: you control the background image (if you want it to span the entire page) usually in the body tag of your HTML. The problem is – the body tag comes BEFORE any other tag in your templates.
For example, take a look at the screenshot of a typical header.php file below. It shows the body tag where you apply a different property value in the CSS file each time you want it unique from another page:

The problem is, in your special template, you’ve already called the get header file:

The Answer – Conditional Tags
WordPress is built is such a way that you can control almost every aspect of your pages. One useful snippet is known as Conditional Tags. Conditional tags can be used in your templates to change what is displayed on a particular page – like the background image. Examples of conditional tags are: is_single(), is_front_page() or is_admin(). For more information on Conditional Tags – see the this link from WordPress Codex library. In our case – we’re going to use the tag is_page_template().
Step 1: Insert the PHP
Open your header.php in your text editor. You’re going to add a conditional statement in inside the opening body id tag. In my case, I’m using three different backround images. Two of them, I want to apply in two individual templates, while the rest of the pages share a default image:
1 2 3 4 5 6 7 8 | <body id=" <?php if (is_page_template('template-diff-bg-demo1.php')){ echo "template-diff-bg-demo1";} elseif (is_page_template('template-diff-bg-demo2.php')){ echo "template-diff-bg-demo2"; }else{ echo "default"; }?>" > |
Explanation: If the template used is named “template-diff-bg-demo1.php”, echo a body id of “template-diff-bg-demo1”, or if template used is named “template-diff-bg-demo2.php” use body id of “template-diff-bg-demo2”, otherwise – just use body id of “default”.
Step 2: The CSS
Open your styles.css. We’re going to match the body ids that are going to be called each time you switch page templates. In our case – we need three different ids:
- body# template-diff-bg-demo1 – a different background
- body# template-diff-bg-demo2 – another background
- body#default – the default background
1 2 3 | body#default { background:url(images/default-bg.jpg) top center no-repeat #000000; } body#template-diff-bg-demo1 { background:url(images/default-bg2.jpg) top center no-repeat #000000;} body#template-diff-bg-demo2 { background:url(images/default-bg3.jpg) top center no-repeat #000000; } |
Conclusion
There you have it. We dynamically changed the body ids of our theme – according to specific page templates. Changing the body ids allowed us style them differently in our stylesheet. This is just one of many possibilities in displaying content (or in our case – style) by using WordPress’ conditional tags. Check out the demo using the code above from this link. Happy Coding!






Great tip. I guess this practice will become a trend in the next gen of WP themes.
i like it, it will be useful for lots of designer
great tip, but what will happen when the site gets heavy traffic, i think this is a great idea for business templates.
keep up the good work mate.
nice tips love them
How would we implement this to take care of a different background on a post as opposed to a page?
great tip. i can definitely use this. keep up the great work.
it sounds great, but i don’t get it working…
when i click a page, only the default background is showing.
I’m a missing something?
thanks!
Hi,
Nice tips !!
I tried it and it worked with firefox but in ie6 none of the background is showing.
my css :
body {
margin: 0 auto; padding:0;
font: 76% arial,sans-serif;
text-align: center;
color: #000000;
}
#default {
background:url(‘img/body_bg.jpg’) center top no-repeat #f2f2f2;
}
#template-basket {
background:url(‘img/body_bg_bb.jpg’) center top no-repeat #f2f2f2;
}
#template-judo {
background:url(‘img/body_bg_judo.jpg’) center top no-repeat #f2f2f2;
}
Oh and i forgot to mention that not only the background image is not showing in ie6 but none of the styling is displayed either.