Support » Fixing WordPress » Listing Child and Grandchild Pages and well as Parent Pages

  • Resolved equalmark

    (@equalmark)


    I have been trying for a long time now, and have come up with almost solutions all the time and was wondering if we could get to the bottom of the display multi level WordPress page menus.

    In plain English here is what I want to do in my sidebar:

    If the current page has children, then display them (only children, not grandchildren). If the current page has no children, then display the current pages siblings, including the current page, unless the current page is a top level page, in which case display nothing.

    I just cannot after months of trying come up with a definitive solution. I am sure that it must be possible with all the WordPress child, parents and ancestor functions.

    Look forward to receiving any help and thanks for your time.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Don’t want to jinx it but I may finally have it. it seems to be working although usually I find something wrong rather quickly. This is what I have at the moment:

    <?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 } else { // if there are no sub pages for the current page ?>
            	<p>There are no sub pages of this page.  What do you want to do?</p>
            <?php } ?>

    Fingers crossed 🙂

    Hi equalmark,

    I’ve been searching high and low for a solution like this when I came across yours. It’s oh so close to what I’m looking for, which I definitely thank you for, but I was wondering if you could help me figure out the one remaining piece! I’d like it to list the sibling pages on the 3rd level page, where there are no more sub-pages – the same list that appears on the 2nd level page.

    To get an example of what I mean, here’s the page hierarchy:

    Level 1 – http://new.usy.org/yourusy/
    Level 2 – http://new.usy.org/yourusy/israel/
    Level 3 – http://new.usy.org/yourusy/israel/hechalutzim/

    I want the same list that appears on level 2 to appear on level 3.

    Can you help?

    Thanks much!

    Nevermind my question – I figured out the answer. Here’s the final code:

    <?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 } ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Listing Child and Grandchild Pages and well as Parent Pages’ is closed to new replies.