Solved

Wordpress Sub nav

Posted on 2012-04-10
4
554 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
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…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…

803 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