Solved

Wordpress Sub nav

Posted on 2012-04-10
4
555 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:john_yourspace
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Dzynit
ID: 37832900
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.)
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 37841050
You could also set a WordPress Custom Menu any way you like and add it as a sidebar widget...
0
 

Accepted Solution

by:
john_yourspace earned 0 total points
ID: 37846176
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


So

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


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

Open in new window

0
 

Author Closing Comment

by:john_yourspace
ID: 37933976
It was the right idea i added the second level with another if statement
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this blog, I will share you some basic tips for content marketing and to rank your website on Google.
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question