
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:

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.
我们能更强,是因为我们站在前人的肩膀上。前人载树,后人乘凉。开始或许只是几颗树苗,以后就是一片森林。今日乘凉,明日或许你我就可以替后人栽树。——好文转载留念
延伸阅读:
- 为wordpress添加相册模板 已经知道如何调用wordpress的原生相册,那要建立一个相册页面就很简单了。新建一个页面,点击添加图片——选择所有要上传图片——点旁边的图片库,选择“插入相册到日志”,再发布页面就OK了。...
- 你所需要了解的 WordPress 2.6 布鲁新安装了Wordpress 2.6后却发现,Windows...
- 如何高亮你的评论背景色 前几天整理评论模板这块,网上胡搜了一通,有了点小心得。...
- wordpress 分类插件——Category Icons 选用图片来识别不同分类,效果总比采用文字的好,难道不是吗~~ ...
- wordpress 各种图片相册插件 自从在JAY那闲逛后,就对如何轻松展示图片产生了浓厚的兴趣,毕竟比起文字,图片往往更能直接抓住某人的眼球~布鲁现在都是先上传到Yupoo,再贴图到文中,图片一多,感觉……快疯了~~ ...
Tagged: navigation, wordpress
See more on 互联网技巧




















.