• So far I have a horizontal mainmenu with the parent pages. Each parent has a child. When I click one of the parents the child-items appear beneath the parent.

    <ul class="span-24" id="nav">
    <?php wp_list_pages('title_li=&depth=1&exclude=18,20'); ?>
    </ul>
    
    <?php if($post->post_parent)
    $children = wp_list_pages("title_li=&depth=1&child_of=".$post->post_parent."&echo=0"); else
    $children = wp_list_pages("title_li=&depth=1&child_of=".$post->ID."&echo=0");
    
    if ($children && is_page()) { ?>
    <ul class="span-24" id="subnav">
    <?php echo $children; ?>
    </ul>
    <?php } else { ?>
    <?php } ?>

    some of the child-pages have children (grandchild). When I click one of the child-pages I get a list of its children. This is being done in a custom template with this code

    <?php
     $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&depth=1");
    ?>
    <div id="sub_sub_menu">
    <ul>
    <?php echo $children; ?>
    </ul>
    </div>

    My problem is that when a click on a grandchild, its parent disappears. I now only see the parent(depth 0) and the grandchild (depth 2). I need to see all three (parent, child and grandchild)

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter pookster

    (@pookster)

    Thnx man! I’ve been testing this around but still can’t get it to work. I’m not sure where to put the entire code. Do I have to put the code for listing the sub pages (depth 2) in the custom template or can I put the entire code in the header.

    I’ve played with both. The problem I’ve been experiencing is that the childpages (with children) disappear as soon as I click on one of its childeren.

    Page 1
    child 1.1
    grandchild 1.1.1
    When on the grandchild page, all items need to be on the screen. No when I’m on the child page and click on grandchild 1.1.1, the page-links of child 1.1 disappears.

    I’ve been using this basic code to list a complete page tree on all parent, child & grandchild pages:

    if( is_page() ) {
    	if( !$post->post_parent ) {
    		$pagelist = '<li><a href="'.  get_page_link($post->ID) .'">' . $post->post_title . '</a>';
    		$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
    		if( $children ) $pagelist .= '<ul>' . $children . '</ul>';
    		$pagelist .= '</li>';
    	}
    	elseif( $post->ancestors ) {
    		// get the top ID of this page. Page ids DESC so top level ID is the last one
    		$ancestor = end($post->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>';
    	}
    	echo $pagelist;
    }
    Thread Starter pookster

    (@pookster)

    I′ve tried that code and it got me in, sort of, the right directions. This is de code I have so far

    <ul class="span-24" id="nav">
    			<?php wp_list_pages('title_li=&depth=1&exclude=18,20'); ?>
    		</ul>
    
    		<?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 class="span-24" id="subnav">
    					<?php echo $children; ?>
    				</ul>
    
    		<?php } ?>

    As you can see, the parent en child pages are in separate ul′s. I now would like the third level also in a separate ul. Right now it′s nested under the second level page. Can′t figure out how. Do you have any suggestions.

    Thread Starter pookster

    (@pookster)

    Let me give you an example of what I’m trying to achieve.

    • I have 5 mainpages (1 to 5)
    • Page 3 has two children (3.1 and 3.2)
    • Child 3.1 also has two children (3.1.1 and 3.1.2)

    I only want to see the children 3.1 and 3.2 when their parent is active. So, only see children when the parent is active. But, when I’m on a childpage 3.1 or 3.2, the parents still need to be visible (1 to 5).

    The same for the children of the children. I only want to see 3.1.1 and 3.1.2 when page 3.1 is active. But, when I’m on page 3.1.1 or 3.1.2 all parents need to be visible ( page 1 to 5 and page 3.1 and 3.2)

    And I realy would like to have alle three levels (3, 3.1 and 3.1.1) in separate ul’s).

    I just can’t get it to work. Hopefully it’s clear what I want.

    Hi, I’m trying to do exactly the same thing as Pookster and can’t figure out what I need to do. I can get close, but not what I’m after. Any advice greatly appreciated.

    I tried Esmi option and it almost works… but it only displays the current parent and its childs. I also want the other parents to be dsplayed in the menu.

    An example:

    HOME
    ABOUT (current page)
    -subpage
    -subpage 2
    LINKS
    CONTACT

    Any help would be highly appreciated.

    Sarah

    (@sazzlemajazzle)

    This sounds like what I’m trying to do as well. I’ve got my main navigation displaying across the top of each page (using wp_nav_menu). One section of the site, called services, has 6 subpages. Each subpage has between 0 and 7 subpages.

    What I’m trying to achieve is, when I click on the services page, the 6 subpages should be displayed in a column on the left of the page (using li tags inside a ul tag). Then, when I click on any of those 6 subpages, if that subpage has any subpages, they should be displayed in the same way (another set of li tags inside a ul tag). If one of those 3rd level pages (sub-subnav) is clicked on, it should display the same as before. Hopefully these examples will help to explain…

    When I click on the services page, in the left column I should see the following:

    Service 1
    Service 2
    Service 3
    Service 4
    Service 5
    Service 6

    When I click on, for example, Service 1, I should see the following:

    Service 1
    Service 1 a
    Service 1 b
    Service 1 c
    Service 2
    Service 3
    Service 4
    Service 5
    Service 6

    …and I should see the same if I click on Service 1 a, Service 1 b or Service 1 c.

    If I click on, for example, Service 2, and it has no subpages, I should see the following:

    Service 1
    Service 2
    Service 3
    Service 4
    Service 5
    Service 6

    Does this make sense? I’d be extremely grateful if anyone can help me with this. I’ve been searching the wordpress forums and searching in general on google and haven’t found the solution yet.

    Thanks,
    Sarah

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘show parent, child and grandchild on grandchild page’ is closed to new replies.