$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Child and Parent Pages list of sub pages|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Child and Parent Pages list of sub pages

matteradmin9PV0评论

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 {

}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far