WordPress Page Navigation – Context Sensitive

Many sites don’t need incredibly deep page navigation though there are situations that justify a hierarchy beyond the typical 2 – 3 levels. Deep page navigation can be cumbersome for layout’s which use top navigation drop-downs so another solution needs to be found. I’m not typically a fan of left hand navigation but there are times when it makes sense and used in conjunction with top navigation it can provide a good user experience. I’d recommend using this technique in conjunction with breadcrumbs (generally a good practice anyway) and only when you have a truly deep, page-heavy site.

The mission is to display sub-pages of the current page you are on in the left navigation and, once you hit the bottom of the hierarchy, to show pages which are parallel to the bottom page within the same branch of the hierarchy. You may also want to employ specific WordPress Page sort order to your pages to have everything organized the way you want it.

The following code will execute exactly the scenario mentioned above in only a few lines of code:

  1. <?php
  2. $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&depth=1");
  3. if ($children == "")
  4. $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0&depth=1");
  5. ?>
  6. <ul>
  7. <?php echo $children; ?>
  8. </ul>
  9. <?php endif; ?>

You will of course need to style the menus to match your site.

Used in conjunction with a standard WordPress top-navigation and breadcrumbs you can easily display context sensitive multi-level WordPress page navigation for your super-complex multi-level site!

If you are also working on a site with more than a few categories and tags you might also want to see the tutorial on Building the Ultimate Website Content Map.

About Scott Ellis

Join me on Google+

Scott Ellis: Founder of vsellis.com, co-founder of DZX Medical and CEO & Managing Partner for pixelterra, web producer for geekbeat.tv,mobile strategy consultant and all around techie. Avid hiker, wine lover, guitar player, scuba diver, traveler, photographer and sports fan. You can find Scott anywhere online by looking for “vsellis” or become friends on twitter @vsellis.

Speak Your Mind

*