Posted: June 14th, 2010 By: Devin Walker
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 show all subsequent child pages. Now what if those child pages have a child? You don't want all menu items to disappear when you click on one of those child pages. That just looks funny. Here's the bit of code that works great for me:
<?php
if ((count($post->ancestors) > 0)
&& ($data = array_reverse($post->ancestors))
&& !is_null($data[0])){
$data = wp_list_pages('title_li=&echo=0&child_of='.$data[0]);
} else {
$data = wp_list_pages('title_li=&echo=0&child_of='.$post->ID);
}
if (strlen($data) != '') {?>
<ul class='nav'><?php echo $data; ?></ul>
<?php } ?>
Related posts:
- How to Use a Facebook Like Button on Any Page The facebook like button is in. I know, you've been seeing it more and more around the web and now you want to implement it on your own website. I'll show you how to do just this using a little snippet of code that will allow you to add a dynamic Facebook "like" button to [...]...
- 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 List Subpages Even if On Subpage 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. [...]...
- 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 [...]...
-
Ladz777
-
Ladz777






