Feedback Follow Me on Twitter!

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.

138 Responses {+}

Trackbacks

  1. Weekly Links - February 16th | Vandelay Website Design
  2. moneyblog » Weekly Links - February 16th
  3. Two Tier Dynamic Navigation For Wordpress
  4. WordPress Hack: Creating a Two-Tiered Navigation Menu
  5. Wp Wordpress » Blog Archive » WordPress Hack: Creating a Two-Tiered Navigation Menu
  6. Links for 2008-03-12 [del.icio.us] : BiliÅŸim & Teknoloji Haberleri | BilisimTekno.NET
  7. Mastering Your WordPress Theme Hacks and Techniques
  8. 精通Wordpress系列教程- 1: 精通Wordpress主题Hacks与技巧 | 帕兰映像
  9. A Vertitable Tsunami of Goodness « Feet up, eyes closed, head back
  10. paidnetpedia.com
  11. WordPress Hack: Creating a Two-Tiered Navigation Menu | [Blog Tutorials]
  12. When To Use Magazine-Style Themes For Blogs? | How-To | Smashing Magazine
  13. Top 5 WordPress Navigation Menu Tutorials
  14. 5个Wordpress导航菜单教程 | 帕兰映像
  15. WP Limits » Blog Archive » 7 Links to Launch the Site
  16. When To Use Magazine-Style Themes For Blogs? | [w3b]ndesign
  17. WordPress Developer’s Toolbox | Developer's Toolbox | Smashing Magazine
  18. Crear menu de navegación de dos niveles para tus secciones « Xyberneticos
  19. links for 2008-09-17 | orioa
  20. WP开发者资源-2.3: WordPress分类和导航菜单 | 帕兰映像
  21. Modifichiamo il menu del nostro Wordpress con uno più web 2.0! - Geekissimo
  22. WordPress Developer’s Toolbox | rafdesign
  23. AndySowards.com :: Web Development Nerdy Daily Links For 09/27/2008 | AndySowards.com :: Professional Web Design, Development, Programming, Hacks, Downloads, Math and being a Web 2.0 Hipster?
  24. Top 5 WordPress Navigation Menu Tutorials | indirazzi.com
  25. Top 5 WordPress Navigation Menu Tutorials | How To WordPress
  26. Extend Wordpress, 5 tips and tricks - Part 1
  27. Wordpress Araçları - İngilizce | indirazzi.com BETA
  28. Crear menu de navegación de dos niveles at Blog Borchani Studios
  29. WordPress Developer’s Toolbox - 山歌好比春江水
  30. WordPress 开发者工具箱1 | Ruijin Ubuntu Blog 生活中的每一点点滴滴......
  31. WordPress 开发者工具箱
  32. 精通WordPress主题制作技巧
  33. 精通Wordpress系列教程- 1: 精通Wordpress主题Hacks与技巧 - pi4.cn 屁事网
  34. 精通WordPress主题制作技巧 - xun`s blog
  35. 逐浪·网络 » Blog Archive » WordPress 开发者工具箱
  36. - WordPress 开发者工具箱(上)【联讯网】
  37. WordPress Developer’s Toolbox | Fusuy.com || Webmaster Accessary Platform
  38. cssframework » WordPress 开发者工具箱【完整版】
  39. 20 WordPress Tricks to Improve Your Blog
  40. 20 Trucchi Per Migliorare Wordpress
  41. Crear menu de navegación de dos niveles para tus secciones at Blog Borchani Studios
  42. 101 Techniques for a Powerful CMS using WordPress | Noupe
  43. WordPress-CMS Hacks and Tricks | Why? Nowhy! DFdou's Blog
  44. Wordpress Blog Services - 101 Techniques for a Powerful CMS using WordPress
  45. 101 Techniques for a Powerful CMS using WordPress | DataViking
  46. 101 Techniques for a Powerful CMS using WordPress
  47. 20 WordPress Tricks to Improve Your Blog | brainstorming magazine | use the brain to get ideas
  48. 101 Techniques for a Powerful CMS using WordPress | Cosa Nuestra Tropical Lounge
  49. 101 Techniques for a Powerful CMS using WordPress | Quest For News, A TUTORIAL Base
  50. 70 Very Useful Wordpress Hacks & Tricks » De Web Times - Sharing Useful Resources.
  51. 零食屋 » Blog Archive » 5个Wordpress导航菜单教程
  52. » 5个Wordpress导航菜单教程 | 零食屋
  53. 11 Trik WordPress Conditional Tags
  54. 5个Wordpress导航菜单教程 | 帕兰映像
  55. Top 5 Tutoriais Menú de Navegación para WordPress | Ajuda Wordpress em Português
  56. WordPress Hack : Creating a Two-Tiered Navigation Menu
  57. WordPress Developer’s Toolbox | 9Tricks.Com - Tips - Tricks - Tutorials
  58. The code to display categories Two-Tiered Conditional Navigation in Wordpress « Graphic Design Blog
  59. 101 Techniques for a Powerful CMS using WordPress « Temas e Dicas para Wordpress
  60.   WordPress Developer’s Toolbox  by Oshoamy
  61. Creating Two-Tiered Conditional Navigation in Wordpress
  62. (摘)精通Wordpress系列教程一 « 李小西的博客
  63. wordpress主题的制作和使用技巧 | 一起走过的日子
  64. geedebook » 11 Trik WordPress Conditional Tags
  65. 101 Techniques for a Powerful CMS using WordPress « WPhub.biz

Leave a Response