Solved

Wordpress Sub nav

Posted on 2012-04-10
4
557 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
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 dynamically set the form action using jQuery.

733 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