Solved

Wordpress Sub nav

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now