Wordpress Sub nav

hi guys,

I am trying to do a side nav like this in wordpress


My code works for level one and two only, how do I get it working for the last level

							$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; ?>
							<?php } ?>

Open in new window

Who is Participating?
john_yourspaceConnect With a Mentor Author Commented:
Hi Dzynit,

This works perfect for level one,

However if I click to level 3 I still need to show the contents of level 1 ,2 ,3


level 1
      level 2
           level 3

This needs to be displayed no matter what level

So perhaps find the top most level parent and go from there like this some what

								if ($post->post_parent)	{
									$parent = $ancestors[$root];
								} else {
									$parent = $post->ID;
                            <ul class="sub-nav">
								<?php wp_list_pages('title_li=&depth=3&child_of='.$parent); ?>

Open in new window

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.

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

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.)
Jason C. LevineNo oneCommented:
You could also set a WordPress Custom Menu any way you like and add it as a sidebar widget...
john_yourspaceAuthor Commented:
It was the right idea i added the second level with another if statement
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.