0

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.

1 Answer 1

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 {

}
5
  • This works for the first level of child pages, but then i have nothing on the second level, with my code i have the right menu on the second level and the wrong one on the first level...
    – reigns1989
    Jan 29, 2019 at 15:47
  • Can you update your post with the IDs of each page from the WP admin (top level, level 1 and level 2) and then echo out the IDs using my updated answer? I just ran the test on a local installation and got the correct nesting of pages. Jan 29, 2019 at 15:49
  • Just tried again and i must have left some rogue PHP in, this has done the job perfectly.
    – reigns1989
    Jan 29, 2019 at 15:50
  • Excellent, glad it's working for you. Jan 29, 2019 at 15:53
  • Thankyou for your help :)
    – reigns1989
    Jan 29, 2019 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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