Blog

Creating Two-Tiered Conditional Navigation in Wordpress

Here is a common navigational scheme, with parent pages on top and child pages (if they exist) on bottom:

twotier

We’ll need code to help us: 1) query the page, 2) determine if there are child pages, and 3) properly highlight both the .current_page_parent and .current_page_item links.

Here is the PHP:

<ul id="nav">
<?php wp_list_pages('title_li=&depth=1'); ?>
</ul>

<?php if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?>
<ul id="subnav">
<?php echo $children; ?>
</ul>
<?php } else { ?>
<?php } ?>

And here is the CSS:

* {
	margin:0;
	padding:0
	}

#nav {
	background:#577da2;
	border-bottom:1px solid #FFF;
	height:32px;
	}

#nav li {
	margin-right:25px;
	}

#nav li, #subnav li {
	float:left;
	list-style:none
	}

#nav a, #nav a:visited {
	color:#FFF;
	text-decoration:none;
	font-weight:bold
	}

#nav a:hover, #nav a:active,
li.current_page_parent a,
li.current_page_parent a:visited,
#nav li.current_page_item a,
#nav li.current_page_item a:visited
        {
	background:#295887
	}

#subnav {
	background:#e6eef7;
	border-top:2px solid #577da2;
	border-bottom:2px solid #cad8e6;
	height:28px;
	}

#subnav li {
	border-right:1px solid #295887;
	padding:0 7px;
	}	

#subnav a, #subnav a:visited {
	color:#295887;
	text-decoration:none;
	font-weight:bold
	}

#subnav a:hover, #subnav a:active,
#subnav li.current_page_item a,
#subnav li.current_page_item a:visited {
	text-decoration:underline
	}

That’s it!

If you’re wondering why the CSS seems overly verbose, it’s to make sure the :active and :hover states display correctly whether or not subpages exist — if they do, the primary nav uses current_page_parent, if they don’t, it resorts to simply current_page_item.

Popularity: 5% [?]

19 Responses »

  1. milo said:

    Cool, helps a lot, this is by far the best solution, just one q:
    does it work with category children as well?

    --February 13, 2008 @ 10:29 am
  2. Hning said:

    Should i use a javascript just like superfish in premium news theme? Or only the script aboves? Thanks before darren. Ops, when mimbro pro will be release? I’ve take a look several premium news theme, but i thought mimbo has a unique value, very very user friendly.

    --February 17, 2008 @ 12:20 pm
  3. Jermayn Parker said:

    The one area I struggle with is the whole navigation for WordPress (as stated in my blog) and how it can be integrated as a CMS properly. So what do we do back-end for the two-tiered conditional navigation to work? Do we organise it by the page parent or page order?

    Thanks and sorry if this seems a little off topic or simple question

    --February 26, 2008 @ 10:08 pm
  4. Darren said:

    So what do we do back-end for the two-tiered conditional navigation to work?

    Simply create pages (and subpages) and the nav code from the article will scale accordingly — that ease of use is what makes WP a great CMS for small and medium sized sites.

    --February 26, 2008 @ 11:00 pm
  5. readywpthemes said:

    Great trick. Thank you…

    --March 12, 2008 @ 7:08 am
  6. Said Salameh said:

    Hi Darryn

    Thanks for the code, great stuff.

    Just would like to know how to make the php code return a three tiered navigation.

    Once again, thks heaps for your tutorials

    Regards
    Said

    --March 15, 2008 @ 8:37 pm
  7. Said Salameh said:

    Opps for the type on your Name

    Regards
    Said

    --March 15, 2008 @ 8:39 pm
  8. people said:

    Wow!! Amazing tip :)
    mmm… it’s possible the same thing with categories and subcategories?

    --March 19, 2008 @ 11:38 pm
  9. Jacky said:

    Great tutorial,thanx

    --April 2, 2008 @ 7:15 am



Trackbacks & Pingbacks

  1. Weekly Links - February 16th | Vandelay Website Design:

    […] Creating Two-Tiered Conditional Navigation in WordPress from Darren Hoyt. […]

    --February 16, 2008 @ 10:27 am
  2. moneyblog » Weekly Links - February 16th:

    […] Creating Two-Tiered Conditional Navigation in WordPress from Darren Hoyt. […]

    --February 16, 2008 @ 12:54 pm
  3. Two Tier Dynamic Navigation For Wordpress:

    […] the full “Creating Two-Tiered Conditional Navigation in Wordpress“ […]

    --February 19, 2008 @ 9:07 am
  4. WordPress Hack: Creating a Two-Tiered Navigation Menu:

    […] accomplish something along these lines, Darren Hoyt has taken the time to explain this in his post Creating Two-Tiered Conditional Navigation in WordPress. Here is the PHP you’ll […]

    --March 12, 2008 @ 7:00 am
  5. Wp Wordpress » Blog Archive » WordPress Hack: Creating a Two-Tiered Navigation Menu:

    […] accomplish something along these lines, Darren Hoyt has taken the time to explain this in his post Creating Two-Tiered Conditional Navigation in WordPress. Here is the PHP you’ll […]

    --March 12, 2008 @ 7:40 am
  6. Links for 2008-03-12 [del.icio.us] : Bilişim & Teknoloji Haberleri | BilisimTekno.NET:

    […] Creating Two-Tiered Conditional Navigation in Wordpress » Darren Hoyt Dot Com WordPress menüsü hack. yani navigasyon menüsünü açılır kapanır bişiler yapmak falan. […]

    --March 13, 2008 @ 12:49 pm
  7. Mastering Your WordPress Theme Hacks and Techniques:

    […] 1)Creating Two-Tiered Conditional Navigation in Wordpress- Here is a common navigational scheme, with parent pages on top and child pages (if they exist) on bottom. […]

    --March 16, 2008 @ 9:40 pm
  8. 精通Wordpress系列教程- 1: 精通Wordpress主题Hacks与技巧 | 帕兰映像:

    […] 1)Creating Two-Tiered Conditional Navigation in Wordpress/建立两层的条件导航菜单- 这里是一个空白的导航方案, 父级菜单在上面,子级菜单在下面。2) Smarter Navigation Menu/更加智能的导航菜单 Mike Cherim 向我们说明如何获得一个更加智能的导航菜单,主要是如何使用条件标签来提供日志标题, 分类日期,和分类名称代替普通的术语. […]

    --March 17, 2008 @ 12:21 pm
  9. A Vertitable Tsunami of Goodness « Feet up, eyes closed, head back:

    […] DarrenHoyt.com: Creating Two-Tiered Conditional Navigation in Wordpress - “Here is a common navigational scheme, with parent pages on top and child pages (if they […]

    --March 22, 2008 @ 5:01 pm
  10. paidnetpedia.com:

    […] 1)Creating Two-Tiered Conditional Navigation in Wordpress- Here is a common navigational scheme, with parent pages on top and child pages (if they exist) on bottom. […]

    --April 12, 2008 @ 3:04 pm


Leave a comment

(required)

(required)



  • rssRSS feed for comments on this post.