WordPress Side Navigation with Parent Heading and Child Sub Pages

admin2025-01-07  7

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!

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!

Share Improve this question asked May 8, 2016 at 23:33 Troy TemplemanTroy Templeman 4217 silver badges24 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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
         )
    );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736262913a849.html

最新回复(0)