Return to Snippet

Revision: 49388
at July 21, 2011 02:00 by zartgesotten


Initial Code
<?php
	//Automatic Submenu
    global $wp_query;
 	//If current page has no parent...
    if( empty($wp_query->post->post_parent) ) 
      {
     echo "";		
     } 
		 
    else 
	//current page has parents
     {
		$me=$wp_query->post->ID;
		$children = wp_list_pages("title_li=&child_of=$me&echo=0"); 
		if ($children) 
		{
			$parent1 = $wp_query->post->post_parent;
			//show menu with current page's siblings
			echo "<ul class='submenus'>";
			wp_list_pages("title_li=&child_of=$parent1&depth=1");
			echo "</ul>";
			
			$parent2 = $wp_query->post->ID;
			//show menu with current page's children
			echo "<ul class='subsubmenu'>";
			wp_list_pages("title_li=&child_of=$parent2&depth=1");
			echo "</ul>";
			
		}
		else
		{
			$parent1 = $wp_query->post->post_parent;
			//show ONLY menu with current page's siblings		
			echo "<ul class='submenus'>";
			wp_list_pages("title_li=&child_of=$parent1&depth=1");
			echo "</ul>";
		}
	}
	?>

Initial URL

                                

Initial Description
This was for a customer. 
Basically it does this:
If a page has no children, it shows ... nothing, 
if a page has a parent, it shows all other children of this parent (siblings) and the current page
if a page has a parent AND children (3rd level), it shows two separate menus, one with the siblings and current page and one with the children of the third level. 
Maybe it helps someone.

Initial Title
3 Level Navigation with Wordpress

Initial Tags
wordpress, navigation

Initial Language
PHP