• I’ve been using the code below to auto display pages and child pages in a sidebar. Our site has a few parent pages and the rest of the pages are all nested within these. Right now the code displays the parent page, child pages, and all grandchildren pages no matter what page a user is on. I would like to modify the code to display the parent page for the title and child pages ONLY in the ul. It would also be cool to be able to leave out a page if needed.

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

    Example of Output
    If a user is on any of the pages below the menu looks the same:
    <h2>Athletics</h2>

    • Baseball
    • HS Baseball
    • Basketball
    • HS Basketball – Boys
    • HS Basketball – Girls
    • JH Basketball – Boys
    • JH Basketball – Girls
    • Cheerleading
    • HS Cheerleading
    • JH Cheerleading
    • Football
    • HS Football
    • JH Football
    • Softball
    • HS Softball
    • Track & Field
    • HS Track & Field – Boys
    • HS Track & Field – Girls
    • Indoor Track
    • JH Track & Field – Boys
    • JH Track & Field – Girls
    • Volleyball
    • HS Volleyball
    • JH Volleyball

    Desired Output Example
    If a user is on the basketball page the menu only looks like this:
    <h2>Basketball</h2>

    • HS Basketball – Boys
    • HS Basketball – Girls
    • JH Basketball – Boys
    • JH Basketball – Girls

    If a user is on the athletic root parent page the menu would look like this:
    <h2>Athletics</h2>

    • Baseball
    • Basketball
    • Cheerleading
    • Football
    • Softball
    • Track & Field
    • Volleyball
Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter ajcke

    (@ajcke)

    Thread Starter ajcke

    (@ajcke)

    Thread Starter ajcke

    (@ajcke)

    Moderator bcworkz

    (@bcworkz)

    The ‘parent’ argument is what you need to only list immediate children, and not grandchildren or greater descendents. This is not documented on the Codex page for wp_list_pages(), but it is on get_pages(), which is called by wp_list_pages(). Provided arguments are simply passed through, so using ‘parent’ should work fine. Just use the same ID as is provided to ‘child_of’.

    You can use the ‘exclude’ argument to list page IDs that you do not want listed. It shouldn’t matter if the IDs excluded will be on the list or not, so just list all pages to exclude no matter what their parents are.

    Thread Starter ajcke

    (@ajcke)

    Thanks bcworkz. Would you mind posting an example by modifying the code I’ve been using above? I’m really clueless.

    Moderator bcworkz

    (@bcworkz)

    No problem ??
    $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&parent=".$post->post_parent."&echo=0&exclude=23,45,67");

    Excludes listing page IDs 23, 45, and 67. Untested though, I hope I got it right. At the risk of adding more confusion, I always encourage people to switch to the preferred array style of arguments. I find it much easier to read and edit. This is the equivalent:

    $children = wp_list_pages( array(
         'title_li' => '',
         'child_of' => $post->post_parent,
         'parent' => $post->post_parent,
         'echo' => 0,
         'exclude' => '23,45,67',
       ));

    Using both child_of and parent together is a bit unorthodox and I’ve never actually used it before. The proper approach per Codex is to replace the child_of line of the array version with this:
    'hierarchical' => 0,

    I tried those codes in my Twenty Thirteen Theme and they don’t work ?? do you have any clue?

    Moderator bcworkz

    (@bcworkz)

    Hi nenifaranto86,

    I went ahead and tested the code on my site with a child of the twentythirteen theme. I placed the code on sidebar-main.php after the dynamic_sidebar( 'sidebar-1' ); line.

    Aside from some CSS issues, the results are as expected, maybe your expectations are not identical? In what way is the code not working for you? What is intended is if the current post is the top level, all descendants are listed. If not top level, only the current page and its siblings are shown, with the parent’s title as a heading. Also note the code is for WP Pages, not Posts, which are not normally hierarchical.

    Since the above code works as expected, it would be best for you to start your own new topic with pertinent information for your site and your desires so as to not confuse things related to the OP’s issue.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘wp_list_pages Output Parent and Child Pages’ is closed to new replies.