• I think this must be a really common problem, but still I haven’t managed to find a solution, that would work for me. I’d like to create a submenu, that displays the whole menu even when on a subpage. This is how a submenu normally works, so it should be possible. I’ve found some plugins (e.g. RYO Folding page list), which does this, but the only problem is, that it also includes the top level links, which I’m already displaying in my horizontal global menu (I have a vertical submenu on the left below it), and I haven’t been able to remove them. Maybe somebody knows how I could hack the plugin or could come up with even a better solution?

    In other words, if I’m on a “subpage 1”, I’d like the menu to include the siblings, the parent and parent’s siblings. Like this:

    Parent sibling 1
    Parent
    –Subpage 1
    –Subpage 2
    –Subpage 3
    Parent sibling 2
    Parent sibling 3

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter wepster

    (@wepster)

    I’ve already tried all of the options listed on that page, and none of the code snippets provided there do, what I’m trying to achieve. I’d like the submenu to keep the structure, that I showed above, even when on a subpage.

    I don’t think this is exactly what you’re looking for but it might give you a head start:

    FUNCTIONS.PHP

    // Return page tree
    function theme_page_tree($this_page) {
    	if( !$this_page->post_parent ) $pagelist = wp_list_pages('title_li=&child_of='.$this_page->ID.'&echo=0');
    	elseif( $this_page->ancestors ) {
    		// get the top ID of this page. Page ids DESC so top level ID is the last one
    		$ancestor = end($this_page->ancestors);
    		$pagelist = wp_list_pages('title_li=&include='.$ancestor.'&echo=0');
    		$pagelist = str_replace('</li>', '', $pagelist);
    		$pagelist .= '<ul>' . wp_list_pages('title_li=&child_of='.$ancestor.'&echo=0') .'</ul></li>';
    	}
    	return $pagelist;
    }

    PAGE.PHP

    <?php if (function_exists('theme_page_tree') && theme_page_tree($post)) :?>
    <div id="showchildren">
    <h3><?php _e('Pages in this section');?></h3>
    <ul>
    <?php echo theme_page_tree($post);?>
    </ul></div>
    <?php endif;?>

    Thread Starter wepster

    (@wepster)

    Thanks, I tried it out, but all I got with this is the top level item (the same one, that is already selected in the global menu) and none of the children. Maybe if in the “elseif” part instead of getting the top ID, I would call the ID from one level below. I’m not sure how to put that into code though.

    I’ve just modified the function slightly so that it show the full page tree even when on the top level parent page:

    // Return page tree
    function theme_page_tree($this_page) {
    	if( !$this_page->post_parent ) {
    		$pagelist = '<li><a href="'.  get_page_link($this_page->ID) .'">' . $this_page->post_title . '</a>';
    		$children = wp_list_pages('title_li=&child_of='.$this_page->ID.'&echo=0');
    		if( $children ) $pagelist .= '<ul>' . $children . '</ul>';
    		$pagelist .= '</li>';
    	}
    	elseif( $this_page->ancestors ) {
    		// get the top ID of this page. Page ids DESC so top level ID is the last one
    		$ancestor = end($this_page->ancestors);
    		$pagelist = wp_list_pages('title_li=&include='.$ancestor.'&echo=0');
    		$pagelist = str_replace('</li>', '', $pagelist);
    		$pagelist .= '<ul>' . wp_list_pages('title_li=&child_of='.$ancestor.'&echo=0') .'</ul></li>';
    	}
    	return $pagelist;
    }

    It’s working fine in my themes.

    Thread Starter wepster

    (@wepster)

    Still no luck here ?? The submenu is still just repeating the selected globalmenu item.

    The following code works perfectly on all pages except a subpage in the deepest level. So if I in the navigation example I gave earlier, go to any of the subpages, the code only displays the siblings of that subpage. Is it possible to easily edit this code to make it work on the subpage?

    if($post->post_parent)
    $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
    else
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    if ($children) { ?>
    <ul>
    <?php echo $children; ?>
    </ul>

    I’ll give a concrete example to make it easier to follow my thoughts. I have a global menu item called “Results”. Under Results I have a submenu with years, e.g. 2010 and 2009. Under e.g. 2010 I have Men, Women and Teenagers. If I choose e.g. Women, I get a menu, that only displays Men, Women and Teenagers.

    I could also separate the level 3 links from the submenu, and display them as a separate menu in the content area instead. When trying out different codes, I’ve run into a version (maybe on that codex page you referred to earlier), that when being on a subpage displayed the parent and the parent’s sibling and none of the subpages. I could use that, if I knew how to add the separate subpage menu to the same page.

    Sorry _ I forgot to mention – you need to place the page.php code inside the Loop.

    Thread Starter wepster

    (@wepster)

    Thanks esmi, now it’s showing the ancestors on the subpage too, but none of the subpages. There is one problem though, it’s repeating the top most level (Results), which is already visible and selected in the global menu. Do you know how I could remove it?

    Is there a way I could produce another menu, that would include only the subpages? This would nicely compliment the menu on subpage, that is now only showing the years.

    it’s repeating the top most level (Results), which is already visible and selected in the global menu. Do you know how I could remove it?

    Use the first code I have for the function. It should only list the top level page when in one of the sub-pages.

    Is there a way I could produce another menu, that would include only the subpages?

    I’m pretty sure that this example will work.

    Thread Starter wepster

    (@wepster)

    I ended up using the following code for the left-side navigation:

    <?php
        if(!$post->post_parent){
            // will display the subpages of this top level page
            $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
        }else{
            // diplays only the subpages of parent level
            $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
    
            if($post->ancestors)
            {
                // now you can get the the top ID of this page
                // wp is putting the ids DESC, thats why the top level ID is the last one
                $ancestors = end($post->ancestors);
                $children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
                // you will always get the whole subpages list
            }
        }
    
    	if ($children) { ?>
    		<ul>
    			<?php echo $children; ?>
    		</ul>
    	<?php } ?>

    And this code for the complimentary navigation inside the content field, when the bottom level of navigation isn’t displaying on the left:

    <?php
    			  if($post->post_parent)
    			  $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
    			  else
    			  $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    			  if ($children) { ?>
    			  <ul>
    			  <?php echo $children; ?>
    			  </ul>
    			<?php } ?>

    I’m taking advantage of the parentpage-id class in the body to hide the complimentary menu from all except the bottom level subpages. I wonder if there’s a dynamic way of displaying the menu only on the lowest level subpages.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Parents and siblings in the submenu (the whole submenu) also on subpage?’ is closed to new replies.