1

I have the following navigational structure in WordPress:

  Page
     Sub Page
        Child Page
        Child Page
        Child Page
  Page
     Sub Page
        Child Page
        Child Page
        Child Page
  Page
     Sub Page
        Child Page
        Child Page
        Child Page

On each Sub Page and Child Page, I want to display the following in the sidebar:

  Sub Page
        Child Page
        Child Page
        Child Page

The closest I've come to this is the following code from here:

<ul>
<?php
global $post;
$current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );      
wp_list_pages( array(
       'title_li' => '',
       'child_of' => $current_page_parent,
       'depth' => '1' )
);
?>
</ul>

However, on Sub Pages it only shows a list of all Sub Pages (without Child Pages) and on Child Pages, it displays a list of Child Pages of that section but the parent Sub Page title is missing.

How do I modify to achieve what I'm looking for?

Thanks!

0

Change the depth parameter to how many levels you want the walker to traverse.

From the docs depth parameter description.

Number of levels in the hierarchy of pages to include in the generated list. Accepts -1 (any depth), 0 (all pages), 1 (top-level pages only), and n (pages to the given n depth). Default 0.

Let us say 2 as the depth level , it outputs both SubPages and it's first level children.

<?php
    global $post;
    $current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );      
    wp_list_pages( array(
           'title_li' => '',
           'child_of' => $current_page_parent,
           'depth' => '2' // Traverses SubPage and it's first level children
         )
    );
  • Thanks @bravokeyl but that doesn't give me what I'm looking for. On Sub Pages it shows a list of ALL Sub Pages (with ALL Child Pages) and on Child Pages, it still displays a list of Child Pages of that section but the parent Sub Page title is still missing. – Troy Templeman May 13 '16 at 11:40
  • @TroyTempleman, are you sure for subpages , all subpages with all child pages are displayed ? childpages shouldn't be displayed. Are you getting correct $current_page_parent ? – bravokeyl May 13 '16 at 12:28
  • Yes, I'm sure. Perhaps it has to do with my navigational structure? It's three levels deep. I do want child pages displayed, just not the ones from other Sub Pages (or their parent Sub Page). I only want Child Pages from the current Sub Page, and the exact same thing on Child Pages. – Troy Templeman May 13 '16 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.