Need some help with wp_list_pages & get_post_ancestors

Hello,
So I have been searching online for something that I think is simple to achieve and I am most likely over thinking it. I have found many related articles on the matter but can't seem to get it to apply to my scenario. I have a sidebar menu on my site that displays a tier of sub-pages. So if you click on "Services" the sidebar shows

Service 1
Service 2
Service 3
Service 4
Service 5

This is perfect, as these are my 5 sub-pages underneath "Services". Now "Service 1, 2 & 3" have anywhere from 5-15 sub-pages under them. What I want to achieve is if you click on "Service 1" in the sidebar and the page loads the sidebar will adjust to show:

Service 1 -- sub-page 1 -- sub-page 2 -- sub-page 3
Service 2
Service 3
Service 4
Service 5

So on the 3rd tier of pages I still want the sidebar to display the main sub-services links but if you are on a sub-service page that has its own sub-pages I would like it to display those sub-pages as well as the original main service sub-pages.

Currently I have only been able to get this show like this:

Click on "Service 1", sidebar menu reflects: - Service 1 -- sub-page 1 -- sub-page 2 -- sub-page 3 - Service 2 - Service 3 - Service 4 - Service 5

Which is good, but then if I click on one of those "sub-pages" the menu updates to only show the sub-pages of that service and not the menu structure that was there before.

Sorry for the long post. I have tried this link using a custom walker: http://wordpress.mfields.org/2010/selective-page-hierarchy-for-wp_list_pages/ I have also tried:

<ul class="sidebar_menu">
            <?php wp_list_pages( array('title_li'=>'','include'=>get_post_top_ancestor_id()) ); ?>
            <?php wp_list_pages( array('title_li'=>'','depth'=>1,'child_of'=>get_post_top_ancestor_id()) ); ?>
        </ul>

and:

<?php
            $ancestor_id=$post->post_parent;
            $descendants = get_pages(array('child_of' => $ancestor_id));
            $incl = "";

            foreach ($descendants as $page) {
               if (($page->post_parent == $ancestor_id) ||
                   ($page->post_parent == $post->post_parent) ||
                   ($page->post_parent == $post->ID))
               {
                  $incl .= $page->ID . ",";
               }
            }
        ?>

        <ul>
            <?php wp_list_pages(array(
                "child_of" => $ancestor_id,
                "include" => $incl,
                "link_before" => "",
                "title_li" => "",
                "sort_column" => "menu_order"
                ));
            ?>
        </ul>

Any help would be greatly appreciated.

  • Danny Albeck

    Hello Jack,

    So technically my link structure is 3 tiers.

    Tier 1: Services (Main Landing Page) (Parent Page)
    Tier 2: Service 1, Service 2, Service 3, Service 4 (Child Pages of Services (Parent Page))
    Tier 3: Service 1 Child Page #1, Service 1 Child Page #2, Service 1 Child Page #3 (Grand Child)

    I would like on my main child pages (Tier 2) to show all of the Tier 2 links, which I do have them doing right now. I only want to show the Tier 3 link if you are active on a Tier 2 link that has child pages (which works properly right now)

    Where it breaks down is once I leave a tier 2 page and go to one of the tier 3 pages I then lose the tier 2 links.

    So I would like the sidebar to stay the same and display the tier 2 links + tier 3 links (if under that specific tier 2 link) even when I am on a tier 3 page (grand child)

    Hope that makes sense :slight_smile:

  • Danny Albeck

    @Jack Kitterhing,

    With some tweaking to that link I get the links to show up but they all display. Not just the sub links to the section your in.

    With this snippet:

    <?php
    					global $post;
    					setup_postdata( $GLOBALS['post'] =& $post_object );
    
    					$ancestor_id=$post->post_parent;
    					$descendants = get_pages(array('child_of' => $ancestor_id));
    					$incl = "";
    
    					foreach ($descendants as $page) {
    					   if (($page->post_parent == $ancestor_id) ||
    					       ($page->post_parent == $post->post_parent) ||
    					       ($page->post_parent == $post->ID))
    					   {
    					      $incl .= $page->ID . ",";
    					   }
    					}
    				?>
    
    				<ul>
    					<?php wp_list_pages(array(
    						"child_of" => $ancestor_id,
    						"include" => $incl,
    						"link_before" => "",
    						"title_li" => "",
    						"sort_column" => "menu_order"
    						));
    					?>
    				</ul>

    I get everything I want with the exception of losing those 2nd tier links when viewing the 3rd tier of links. Any other ideas?

  • Danny Albeck

    Finally figured it out. Little tricky and took a lot of coffee to figure out, below is the snippet of code in hopes it will help someone else looking to achieve this:

    <?php
    						$children = wp_list_pages('depth=1&title_li=&child_of='.$post->ID.'&echo=0');
    						$current_page = wp_list_pages('title_li=&include='.$post->ID.'&echo=0');
    						$grandparent=0;
    							if($post->post_parent):
    								$parent = wp_list_pages('title_li=&include='.$post->post_parent.'&echo=0');
    								$siblings = wp_list_pages('depth=1&title_li=&child_of='.$post->post_parent.'&exclude='.$post->ID.'&echo=0');
    								$parent_children = wp_list_pages('depth=1&title_li=&child_of='.$post->post_parent.'&echo=0');
    								$parent_post = get_post($post->post_parent);
    								$grandparent = $parent_post->post_parent;
    								$parent_siblings = wp_list_pages('depth=1&title_li=&child_of='.$grandparent.'&echo=0&exclude='.$post->post_parent);
    							endif;
    							if(!$post->post_parent && $children):
    					?>
    
    					<ul class="sidebar_menu">
    						<?php echo $children;?>
    					</ul><!-- /.sidebar_menu -->
    
    					<?php elseif($post->post_parent && $children): ?>
    
    						<ul class="sidebar_menu">
    							<?php echo $current_page;?>
    								<ul class="sidebar_menu children">
    									<?php echo $children;?>
    								</ul><!-- /.children -->
    							<?php echo $siblings;?>
    						</ul><!-- /.sidebar_menu -->
    
    					<?php elseif($grandparent && $post->post_parent && !$children): ?>
    
    						<ul class="sidebar_menu">
    							<?php echo $parent;?>
    							<ul class="sidebar_menu children">
    								<?php echo $parent_children;?>
    							</ul><!-- /.children -->
    							<?php echo $parent_siblings;?>
    						</ul><!-- /.sidebar_menu -->
    
    					<?php elseif(!$grandparent && $post->post_parent && !$children): ?>
    
    						<ul class="sidebar_menu">
    							<?php echo $parent_children;?>
    						</ul><!-- /.sidebar_menu -->
    
    					<?php endif;?>

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.