php - Child and Parent Pages list of sub pages

admin2025-06-03  3

I have a parent page called services with children, each service child has child pages within it.

On the first level child page i want to show all of the pages below it in. When i click through to one of those pages, i want that same menu to display, with all of the pages that are the same level as the one i am on showing.

i currently have this code:

    global $post;
$subpages = wp_list_pages( array(
    'echo'=>0,
    'title_li'=>'',
    'depth'=>1,
    'child_of'=> ( $post->post_parent == 0 ? $post->ID : $post->post_parent)
));
if ( !empty($subpages) ) {
?>
<div class="services-sub-menu"> 
<?php
    if ( $post->post_parent != 0 ) {
        echo '<p class="parent-link"><a href="'. get_permalink($post- 
>post_parent) .'"><em>'. __('Back to') .' '. get_the_title($post- 
 >post_parent) .'</em></a><p>';
    }
    echo '<ul>';
    echo $subpages;
    echo '</ul>';

    ?>

</div>

<?php
} else {

}

This works in that is shows the current level sub items. However, on first level children i want to show the children below, on second level children i want to show children on the same level.

Thankyou in advance for your help, much appreciated.

I have a parent page called services with children, each service child has child pages within it.

On the first level child page i want to show all of the pages below it in. When i click through to one of those pages, i want that same menu to display, with all of the pages that are the same level as the one i am on showing.

i currently have this code:

    global $post;
$subpages = wp_list_pages( array(
    'echo'=>0,
    'title_li'=>'',
    'depth'=>1,
    'child_of'=> ( $post->post_parent == 0 ? $post->ID : $post->post_parent)
));
if ( !empty($subpages) ) {
?>
<div class="services-sub-menu"> 
<?php
    if ( $post->post_parent != 0 ) {
        echo '<p class="parent-link"><a href="'. get_permalink($post- 
>post_parent) .'"><em>'. __('Back to') .' '. get_the_title($post- 
 >post_parent) .'</em></a><p>';
    }
    echo '<ul>';
    echo $subpages;
    echo '</ul>';

    ?>

</div>

<?php
} else {

}

This works in that is shows the current level sub items. However, on first level children i want to show the children below, on second level children i want to show children on the same level.

Thankyou in advance for your help, much appreciated.

Share Improve this question asked Jan 29, 2019 at 15:05 reigns1989reigns1989 171 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You need to work out what page level you are on first, so that you know the correct ID to pass to the child_of argument of wp_list_pages. Give this a try:

global $post;
$page_level = 0;
$post_parent = null;

// Work out what page level we're on
if($post->post_parent > 0) {
    $post_parent = get_post($post->post_parent);
    $page_level = ($post_parent->post_parent == 0 ? 1 : 2);
}

// Updated with page IDs
echo 'Current page: '.$post->ID.'<br />';
echo 'Current parent: '.$post->post_parent.'<br />';
echo 'Current level'.$page_level.'<br />';
echo 'Loading pages from '.($page_level == 2 ? $post->post_parent : $post->ID).'<br />';

$subpages = wp_list_pages(array(
    'echo'     =>0,
    'title_li' =>'',
    'depth'    =>1,
    'child_of' => ($page_level == 2 ? $post->post_parent : $post->ID)
));


if ( !empty($subpages) ) {
?>
<div class="services-sub-menu"> 
<?php
    if ( $post->post_parent != 0 ) {
        echo '<p class="parent-link"><a href="'. get_permalink($post->post_parent) .'"><em>'. __('Back to') .' '. get_the_title($post->post_parent) .'</em></a><p>';
    }
    echo '<ul>';
    echo $subpages;
    echo '</ul>';

    ?>

</div>

<?php
} else {

}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748964945a315214.html

最新回复(0)