WordPress Side Navigation with Parent Heading and Child Sub Pages

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!

Topic wp-list-pages Wordpress

Category Web


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
         )
    );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.