Feedback

Darren Hoyt Dot Com | Design, Development, CSS, CMS, WordPress

Use Body ID/Class to Control Wordpress Page Elements

Long before Wordpress came along, it was considered smart CSS to attach an ID or class to the <body> tag of your webpages. For the homepage layout, you might use:

<body id="home">

For interior pages layouts, you’ll want a separate ID, plus a class which controls the styling of individual pages:

<body id="interior" class="about-us">

This technique can be powerful for turning sidebars on and off, dynamic menu highlighting, color coding, smart navigation — plenty has already been published on the advantages.

The Problem

Wordpress allows for conditionals and custom Page templates, but that often requires the manual creation of extra templates and PHP statements. Wouldn’t it be nice to control page elements through one single css document and one single Page template?

The Solution

This week I did some experimenting and put together a conditional that checks for homepage vs. interior page, and for the latter, creates a class name based on the page slug. Open “header.php” and replace the <body> tag with this:

<body <?php if (is_home()) { ?>
id="home"<?php } else { ?>
id="interior" class="<?php echo $post->post_name; ?>"<?php } ?>>

For me, this bit of code (which I wish came default) is another link in the chain toward exploiting Wordpress’s potential as a CMS.

37 Responses {+}

Trackbacks

  1. del.icio.us links fra 16. januar, 2008 » superevil.org
  2. A Site Redesign for Spring 2008 | DarrenHoyt.com
  3. Elliot Jay Stocks » Wordpress tutorial:how to apply a dynamic body class or ID
  4. A Quick Wordpress Function to Add a Dynamic Body ID/Class to Your Theme » Papertree Design
  5. Definitive Sticky Posts Guide for WordPress 2.7 | Webtrendblog.com
  6. CoffeeBear.net » Blog Archive » Updating VectorLover Theme
  7. Mimbo 3.0 Released | Darren Hoyt Dot Com
  8. Mimbo Pro 2.0 Released » Pro Theme Design
  9. Xtremegang » Blog Archive » Sticky Posts Guide for WordPress 2.7

Leave a Response