Posted: May 25th, 2010 By: Devin Walker
I'm building a new site and the navigation on it requires me to use a different bit of code for parent and child pages. I thought I'd share the code for those out there building a similar navigation. Here's a handy bit of code that you can use to display all subpages on a subpage. For instance, you have your pages setup like so:
Page: Fruit
subpage: Banana
subpage: Apply
subpage: Orange
On Parent Page
If you were wanting to show a navigation menu of all subpages in the Fruit category you could use the following bit of code:
<?php
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if ($children) { ?>
<ul id="mainNav">
<div id="locationsNavHead">choose a product</div>
<?php echo $children; ?>
</ul>
<?php } ?>
On Subpages
For child, or subpages, we could use the following bit of code:
<div id="sidebar">
<?php
//list subpages even if on a subpage
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="mainNav">
<div id="locationsNavHead">choose a product</div>
<?php echo $children; ?>
</ul>
<?php } ?>
</div>
If you are confused at what this all means please comment below and check out wp_list_pages() over at the WordPress Codex
Related posts:
- WordPress Heirarchical Sidebar Page Menus Let's say you want to show a hierarchical sidebar menu that displays a listing of current subpages. This is easy enough, but let's say that you have a top menu that shows all top-level pages. Once you click on a top level page from that menu it will take you there and in the sidebar [...]...
- WordPress Alternating Classes It's often very useful to have alternating classes in for styling purposes. Let's say you want every other row to be a darker shading, this is very common and I'll show you how to implement this in WordPress using a simple bit of code. For my example here I will show you how to create [...]...
- Link separators for wp_list_pages() code snippet WordPress makes it very easy to dynamically create menus from pages and categories with the use of the function wp_list_pages() and wp_list_cats()... but what if you want to have separators in your menus? Here's a bit of code that will do just that: wp_list_pages Separators Please note: I got this code from Rares Comes (very [...]...
- WordPress Loop: If Parent or Child of Page or Category The WordPress loop is extremely useful when developing customized WordPress solutions. One bit of code I've had to use often is one that will display a set of code if the page or post is a parent of child of a certain category. This is an easy way to display, output, or show data on [...]...
- WordPress Lavalamp Navigation Tutorial Lavalamp is an awesome jQuery plug-in that makes really cool effects for WordPress-based menus and navigation links, and as a bonus it is very lightweight. It can be a bit tricky to implement in WordPress, but after you read this tutorial hopefully you'll be implementing it with ease. Follow along and let's get your site's [...]...
-
http://www.tehacesver.com Jorge
-
Jamie
-
Musti






