Tech Hive

Blog on Wordpress, Design, Usability, Accessibility and Standards

  • Content
  • Sidebar
  • Footer
  • « New Wordpress Upgrade
  • Proudly Pinoy Logo Contest »

Creating Active Tabs in WordPress

Leave a response | Trackback |

Having active tabs in websites help in usability for it shows where the person currently is. For that to happen, we should make it a point that the active navigation and the rest of the body have connection to one another. (See this blog and Last Leaf as examples.)

Doing this in Wordpress is quite easy, you only have to be familiar with the very basic if and else PHP statements. Let’s go to the coding!


<ul>
    <li class="home<?php if(is_home()) {?> active
       <?php } else {} ?>">
    <a<?php if(is_home()) { ?>
       <?php } else { ?> href="<?php bloginfo('URL'); ?>">
       <?php } ?>>Home
    </a></li>
  </ul>

I would have added several list items as well but, well, it may look confusing. Anyway, this single example is enough (I think) to show how it’s done — if you still don’t know how that is.

Let’s dissect the code:


<li class="home<?php if(is_home()) {?> active<?php } else {} ?>“>

This code means that if the current page is the home page then it should also show the class “active” otherwise, “home” class is enough. The active class is important because this will make the active tab for that particular link item.


<a<?php if(is_home()) {} else { ?> href=”<?php bloginfo(’URL’); ?>”><?php } ?>>Home</a>

Now this code means that if the current page is the homepage then it shouldn’t display anything within the <a> tag otherwise, it should be linked to the blog’s URL specified by this bit bloginfo('URL');.

The idea here is that if you are on the current page, the user should not be able to click on a link that will bring them back to that very same page they are on as it is a usability issue as well (see “Where Am I?” by Derek Powazek).

For the archives, this is what I have:


<ul>
    <li class="archives
       <?php if(is_single() || is_archive() || is_page('1')) {?>
       active<?php } else {} ?>">
    <a<?php if(is_single() || is_archive() || is_page('1')) { ?>
       <?php } else { ?> href="<?php bloginfo('URL'); ?>">
       <?php } ?>>Archives</a></li>
</ul>

The idea in this one is to define the pages that should be under Archives and that includes the single post, monthly archives, category archives and your archives page — if you have one. I think it is good practice for you to define your page in ID instead of the page’s title because if you’re anything like me, you might feel the urge to change the title every now and then but the ID remains the same. That is one less thing to worry about for you.

Another thing I do is that I usually put this in a separate PHP file so I could easily call on this no matter what theme I use. I assume that your site’s navigation will not change overnight so having it in a different file and just adding an include where you want to show it is fine. Plus, your header file (or sidebar) will look less cluttered and you will be able to identify which is which. A note though, make sure that the list item is all in one line, PHP code and all so the result will be something like the one below:


<li class="archive active"><a>Archive</a></li>

Easy isn’t it?! Make your site look good and usable.

Edit (Aug. 12, 2007) — added the missing apostrophe as noticed by Adam C Thanks Adam!

One Response to “Creating Active Tabs in WordPress”

  1. Adam C July 20, 2007 at 11:49 pm

    There is an error in the “Archives” code snippet. Insert an apostrophe after the argument to “is_page” in the first if statement

    is_page(’1)) –> is_page(’1′))

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Pages

  • About the Author
  • Archives
  • Contact
  • Site Map

Site5 $5 Hosting Deal
Come in, we're hiring

Full-time and freelance job opportunities available at Authentic Jobs:

Post a job and reach web professionals everywhere.

Archives

  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007

Categories

  • Accessibility
  • CSS Tips
  • Gmail
  • HTML
  • Online Finds
  • Online Tools
  • Tutorial
  • Updates
  • Usability
  • WordPress
  • Wordpress Themes

Tech Hive is proudly powered by WordPress
Spartan by Mae Paulino, a Filipina Web Designer