WordPress.org

Ready to get started?Download WordPress

Forums

How to show grandchildren when on a grandchild page (11 posts)

  1. addicottweb
    Member
    Posted 3 years ago #

    I need help showing the grandchildren of a child page when on a grandchild page.

    Here is what I have so far and is working up to that point:

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

    Here is what I am trying to do:

    When on a parent page with child pages: Show children
    When on a child page with grandchildren: Show grandchildren
    When on a grandchild page: Show grandchildren of above scenario

    I've been looking all over the forums and on Google and haven't found anything that will let me do this.

    Any help is appreciated.

    Thanks!

  2. esmi
    Forum Moderator
    Posted 3 years ago #

  3. addicottweb
    Member
    Posted 3 years ago #

    Thanks - I did come across that, and I think it should work, but I'm a bit confused about how to actually plug the code into my theme. I want to list the pages generated in an list, so how would I do that? What I mean is, in the example code above, there's the:

    <ul>
      <?php echo $children; ?>
      </ul>
      <?php } ?>

    which obviously generates the list. But in the code that I think I want:

    <?php
    $output = wp_list_pages('echo=0&depth=1&title_li=<h2>Top Level Pages </h2>' );
    if (is_page( )) {
      $page = $post->ID;
      if ($post->post_parent) {
        $page = $post->post_parent;
      }
      $children=wp_list_pages( 'echo=0&child_of=' . $page . '&title_li=' );
      if ($children) {
        $output = wp_list_pages ('echo=0&child_of=' . $page . '&title_li=<h2>Child Pages</h2>');
      }
    }
    echo $output;
    ?>

    I don't see that, or know what to do with it.

  4. esmi
    Forum Moderator
    Posted 3 years ago #

    Replace the first block of code above by the second block in your template file.

  5. addicottweb
    Member
    Posted 3 years ago #

    Great, thanks. That worked once I did that, but it was generating the list of all the sub-pages and sub-sub-pages on the 2nd level pages. So, I modified it as follows so that on the 2nd level page it displays just the child pages of that page. Here's the code:

    <?php $output = wp_list_pages('echo=0&depth=1&title_li=<h2>Top Level Pages </h2>' );
    			  if (is_page( )) { $page = $post->ID;
    			  if ($post->post_parent) { $page = $post->post_parent; }
    
    			  $children=wp_list_pages( 'echo=0&child_of='.$post->post_parent.'&title_li=&depth=1' );
    
    			  if ($children) { $output = wp_list_pages ('echo=0&child_of='.$post->ID.'&title_li=<h2>In This Section</h2>&depth=1'); } } echo $output; ?>

    I have one more question though: on 3rd level pages, how do I get a list of the sibling pages to display? (Basically the same list as appears on the 2nd level page.)

  6. esmi
    Forum Moderator
    Posted 3 years ago #

    functions.php:

    function my_page_tree($this_page) {
    	$pagelist = '';
    	if( !$this_page->post_parent ) {
    		$children = wp_list_pages('title_li=&child_of='.$this_page->ID.'&echo=0');
    		if( $children ) {
    			$pagelist .= '<li class="current_page_item"><a href="'.  get_page_link($this_page->ID) .'">' . $this_page->post_title . '</a>';
    			$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;
    }

    page.php:

    <?php
    if( function_exists( 'my_page_tree') && my_page_tree($post) != '' ) :?>
    <ul>
    <?php echo my_page_tree($post);?></ul>
    <?php endif;?>
  7. addicottweb
    Member
    Posted 3 years ago #

    Thanks so much for the suggestion, I appreciate it. Unfortunately, that didn't work like what I wanted - it was generating the full page hierarchy on each page.

    BUT, I have been looking around, and think I may have found a solution. The easiest way to illustrate is probably to show you the URLs of what I mean.

    1st level - http://new.usy.org/yourusy/
    2nd level - http://new.usy.org/yourusy/communications/
    3rd level - http://new.usy.org/yourusy/communications/regional-communications-vps/

    When I use this code:

    <?php
    // If CHILD_OF is not NULL, then this page has a parent
    // Therefore, list siblings i.e. subpages of this page's parent
    if($post->post_parent){
        wp_list_pages('title_li=<h2>In This Section:</h2>&child_of='.$post->post_parent);
        }
    
    // If CHILD_OF is zero, this is a top level page, so list subpages only.
    else{
        wp_list_pages('title_li=<h2>In This Section:</h2>&depth=1&child_of='.$post->ID);
        }
    ?>

    it works as intended on the 1st and 3rd level pages, but not the 2nd. There's also another bit of code I found:

    <?php $output = wp_list_pages('echo=0&depth=1&title_li=<h2>Top Level Pages </h2>' );
    			  if (is_page( )) { $page = $post->ID;
    			  if ($post->post_parent) { $page = $post->post_parent; }
    
    			  $children=wp_list_pages( 'echo=0&child_of='.$post->post_parent.'&title_li=&depth=1' );
    
    			  if ($children) { $output = wp_list_pages ('echo=0&child_of='.$post->ID.'&title_li=<h2>In This Section</h2>&depth=1'); } } echo $output; ?>

    which worked on the 1st and 2nd levels, but not the 3rd.

    I just can't figure out how to combine those two codes so that they work as intended on all 3 levels. I'm not so much of a coder, so any help you can provide would be much appreciated.

  8. addicottweb
    Member
    Posted 3 years ago #

    FYI, for anyone who is looking through the above conversation - I solved the problem. Here's the code to achieve what I was describing above:

    <?php global $post; $thispage = $post->ID; // grabs the current post id from global and then assigns it to thispage ?>
            <?php $pagekids = get_pages("child_of=".$thispage."&sort_column=menu_order"); // gets a list of page that are sub pages of the current page and assigns then to pagekids ?>
            <?php if ($pagekids) { // if there are any values stored in pagekids and therefore the current page has subpages ?>
            	<ul>
            		<?php wp_list_pages("depth=1&title_li=&sort_column=menu_order&child_of=".$thispage); // display the sub pages of the current page only ?>
                </ul>
            <?php } elseif($post->post_parent)
    				$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); if ($children) { // if there are no sub pages for the current page ?>
      <ul>
      <?php echo $children; ?>
      </ul>
            <?php } ?>
  9. asjbak
    Member
    Posted 3 years ago #

    Thank you addicottweb!
    I did look around for hours for this solution!
    100+ kudos for you :)

    If a page has children > show children
    If on the childpage and the childpage has grandchildren > show grandchildren
    If on the grandchild > show rest of grandchildren

  10. mmejiaramirez
    Member
    Posted 3 years ago #

    Great addicottweb!
    This is very useful for me :)

  11. gaberosser
    Member
    Posted 2 years ago #

    SO helpful @andicottweb! Forever thankful. Most folks would have just been happy to have a solution and forgotten about the post since they didn't need any further answers. You're a trooper for posting back the solution.

    big thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic