john_yourspace
asked on
Wordpress Sub nav
hi guys,
I am trying to do a side nav like this in wordpress
Page
Subpage
sub-sub-page
My code works for level one and two only, how do I get it working for the last level
I am trying to do a side nav like this in wordpress
Page
Subpage
sub-sub-page
My code works for level one and two only, how do I get it working for the last level
<?php
$has_subpages = false;
// Check to see if the current page has any subpages
$children = wp_list_pages('&child_of='.$post->ID.'&echo=0');
if($children) {
$has_subpages = true;
}
// Reseting $children
$children = "";
// Fetching the right thing depending on if we're on a subpage or on a parent page (that has subpages)
if(is_page() && $post->post_parent) {
// This is a subpage
$children = wp_list_pages("title_li=&include=".$post->post_parent ."&echo=0");
$children .= '<li><ul class="inside">';
$children .= wp_list_pages("title_li=&child_of=".$post->post_parent ."&echo=0");
$children .= '</li></ul>';
} else if($has_subpages) {
// This is a parent page that have subpages
$children = wp_list_pages("title_li=&include=".$post->ID ."&echo=0");
$children .= '<li><ul class="inside">';
$children .= wp_list_pages("title_li=&child_of=".$post->ID ."&echo=0");
$children .= '</li></ul>';
}
?>
<?php // Check to see if we have anything to output ?>
<?php if ($children) { ?>
<ul class="sub-nav">
<?php echo $children; ?>
</ul>
<?php } ?>
You could also set a WordPress Custom Menu any way you like and add it as a sidebar widget...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It was the right idea i added the second level with another if statement
Then add css in your styles.css to adjust the margin/indent you want.
Open in new window
(If you need to set more options for the menu, such as order or excluding pages, the function reference is pretty easy to follow here http://codex.wordpress.org/Function_Reference/wp_list_pages.)