skip to content rich footer

stevenclark.com.au

subscibe to the StevenClark.com.au rss feed

WP Dynamic Body ID and Current Page

Referring back to an old post on using a Dynamic Body ID in WordPress you might notice that anything in WordPress that is not a page is actually a part of the blog - including search, single, archive and the posts page. The following code should refresh your memory on how to use a dyanamic body ID. If it’s not a page then give the body an ID of News, else if it is a page with the following titles then the appropriate body ID of the title is used, otherwise just insert the body tag.

<?php if ((!is_page())) { ?>
<body id=”News”>
<?php } elseif ((is_page(’Home’)) || (is_page(’Studio’)) || (is_page(’Support’)) || (is_page(’About’)) || (is_page(’Contact’)))
{ ?>
<body id=”<?php the_title(); ?>”>
<?php } else { ?>
<body>
<?php } ?>

This is a handy base for doing something useful so I thought a simple continued snippet or two might open your mind to the possibilities. For example, you might like to highlight the current page in your navigation. Even better, you could highlight the current page but keep the News (blog) link highlighted while users are on all the blog related pages such as search, archive etcetera. Sounds logical and it’s easy enough to achieve (This example comes from the Hunter Island Press theme).

We’ll start by putting a few more IDs into the links on the horizontal navigation bar. In this case the navigation is made up of a simple unordered list of links. Note that the ID is placed directly on the unordered list and not on a containing div. You don’t need to put a block level element into a div container, it’s better to just work directly with the list.

<ul id="navbar">
    <li><a id="homelink" href="#">Home</a></li>
    <li><a id="studiolink" href="#">Studio</a></li>
    <li><a id="supportlink" href="#">Support</a></l>
    <li><a id="aboutlink" href="#">About</a></li>
    <li><a id="newslink" href="#">News</a></li>
    <li><a id="contactlink" href="#">Contact</a></li>
</ul>

The next task is to style the horizontal navigation bar without the current page tab highlighted. When you get this working you can worry about the current tab later.

ul#navbar {
position: static;
margin: 0;
padding: 0;
width: 100%;
display: block;
list-style-type: none;
background-color: #000;}ul#navbar li { display: inline; }

ul#navbar li { display: inline; }

ul#navbar li a {
margin: 0;
padding: 9px 0;
width: 16.6%;
min-width: 70px;
height: 40px;
float: left;
border-bottom: 4px solid #000;
font-size: .9em;
font-family: helvetica, verdana, arial, sans-serif;
color: #fff;
text-align: center;
text-decoration: none; text-transform: uppercase;
background: #000; }
ul#navbar li a:hover {
border-bottom: 4px solid #8F0000;
background: #222; }

As a snippet goes you can take or leave a lot of what is in there (including the min-width that applies to the fluid layout of Hunter Island Press). But all that’s left for us to achieve is the highlighting of the current tab - specifically so that users entering the news get a consistent tab for the section. We achieve this by associating the body ID with the link ID in the navigation bar. If any of the following selectors are present the background and bottom border will be changed to appear as the current tab.

body#Home li a#homelink,
body#Studio li a#studiolink,
body#Support li a#supportlink,
body#About li a#aboutlink,
body#News li a#newslink,
body#Contact li a#contactlink {
background: #222;
border-bottom: 4px solid #8f0000; }

Hopefully you can take something about using WordPress away from this tutorial and extend the concept of the dynamic body ID further. Enjoy.

Comments are closed.

Stand Up Guy

Steven Clark Steven Clark - the stand up guy on this site

An icon for overweight middle aged bogun-geek web designers. A lego block in a Meccano world. A synergy of tattoos, memories of bare knuckle fist fights, and old episodes of Star Trek. My name is Steven Clark and I'm a highly opinionated web designer with a few good ideas. I'm too old for fist fights.

My Photography Blog

My photography blog Walk a Mile in my Shoes is back up and running. Due to bandwidth issues it's only one image at a time and not full text in the RSS feed. It's licensed under creative commons , meaning not for commercial use and you need to attribute, otherwise drop me a line via the contact form on this site.

My Links Blog

You might also like to check out my links blog over at Nortypig.com to learn more about everything worth mentioning.

My Illustrations

Currently I only have a static page for illustrations but if time allows I'd like to start another illustration blog.

Declaration of Independence

Site Content

Developed and published by Steven Clark

Site Supporters

Hosted by Brett at Tashosting

skip to top of page

Currently Reading

Here Comes Everybody by Clay Shirky (cover)

Clay Shirky's Here Comes Everybody: The Power of Organizing without Organizations has been on my bookshelf for the last few months literally screaming to be read. In fact, I'm wondering how I got so sidetracked to have reached the end of the year without having consumed it. The message of the book is an area of my own fascination, the effects that our new technologies have on the way we relate to each other, and how we're now empowered in ways that were historically unheard of (or not even conceived of) not too long ago.

I'm a small town boy who grew up in the seventies, graduating high school in 1979. The world was slower - how did we survive without Wikipedia? Without MSN or Facebook? Nowdays we have flashmobbing and blogging and constant connection.