This is a conditional body tag for the header.php in your WordPress Theme. Naturally, be sure to add the opening and closing php tags.
if (is_home()) {
// we're on the home page so just show a basic body tag
echo "<body>";
} else {
// every other page can have this ID in the body tag
echo "<body id='nothome'>";
} // That's all, folks!
Why would I want this? Well, lets say you are using your WordPress site for more than just a blog. And the home page body tag has a different background style than any other page, this would allow you to apply different styles to them.
I just used this for a recent project. The home page has a large “feature story”, and the body background image is lower than any other page, to line things up with the feature story box.
But why not just use a div? No reason, really. This just leaves less code in your source.
I’m using the Classy Body plugin for that:
http://www.alistercameron.com/2007/01/04/wordpress-plugin-classybody/
but I do love conditional tags.
# Posted at 1:22 pm on Jan 24th, 2008indeed, that is a handy plugin! thanks for the link s1mone.
# Posted at 3:01 pm on Jan 24th, 2008Chad, conditional comments are life savers (well, almost!). They make it so much easier to wrangle WP in to doing things it wasn’t originally intended to do.
I have some ideas for a conditionally commented script which would show different sidebar content based on the length of a post. Do you know if this is possible? It looks as though WordPress doesn’t have conditional comments for the post body, but it could make a handy script.
# Posted at 11:35 pm on Jan 26th, 2008Interesting Idea, Neil.
Are you talking about something along the lines of this:
if (this_many_posts()) {
show "all of the sidebar";
} else {
show "show only some of the sidebar";
}
Cos with the sidebar being outside of the loop, that might be a tricky one. I know that I’m not savvy enough to pull something like that off…
# Posted at 9:53 am on Jan 27th, 2008Sorry for the much delayed response!
Yes, an if statement would do the trick. It would probably need to be based on the character length of the post, although I don’t know how workable that is, e.g. will 2000 character posts always be a similar length in pixels?
It would be great as a plugin with a dashboard interface through which you could determine the cut-off points for introducing new content in the sidebar. Might hit the database pretty hard if you’re counting the characters in each post…
p.s. just clicked on your lightbox pop-up pic of the north Idaho panhandle, man I’m jealous! Do you have a view from your office?
# Posted at 10:50 pm on Feb 25th, 2008The view from my office isn’t as spectacular as the view in that photo. maybe it will be someday, if i can get a office in town.
# Posted at 8:15 pm on Mar 1st, 2008