Link to home
Start Free TrialLog in
Avatar of john_yourspace
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



 <?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 } ?>

Open in new window

Avatar of Heather Ritchey
Heather Ritchey
Flag of United States of America image

It seems that you're making it harder than you need to. If you use the regular wordpress call adding in depth=3, it will give you the menu you display in your example without such extensive coding.
Then add css in your styles.css to adjust the margin/indent you want.

<ul>
<?php wp_list_pages('title_li=&depth=3'); ?>
</ul>

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.)
Avatar of Jason C. Levine
You could also set a WordPress Custom Menu any way you like and add it as a sidebar widget...
ASKER CERTIFIED SOLUTION
Avatar of john_yourspace
john_yourspace

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of john_yourspace
john_yourspace

ASKER

It was the right idea i added the second level with another if statement