Php 在wp_列表_页面中保留页面层次结构,即使是在子页面或孙子页面上

Php 在wp_列表_页面中保留页面层次结构,即使是在子页面或孙子页面上,php,wordpress,custom-wordpress-pages,Php,Wordpress,Custom Wordpress Pages,我有以下页面结构,我需要在有子页面的页面上显示相同的结构: - Childpage - - Grandchildpage - - Other Grandchildpage - Other Childpage 以下代码用于在page.php上显示结构: <ul class="sidemenu-list"> <?php if($post->post_parent) $children = wp_list_pages("title_li=&am

我有以下页面结构,我需要在有子页面的页面上显示相同的结构:

- Childpage
- - Grandchildpage
- - Other Grandchildpage
- Other Childpage
以下代码用于在page.php上显示结构:

<ul class="sidemenu-list">
    <?php
    if($post->post_parent)
        $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=1");
    else
        $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=1");
        if ($children) { ?>
        <ul>
            <?php echo $children; ?>
        </ul>
    <?php } ?>
</ul>
当我在“孙子页面”时,我只得到:

- - Grandchildpage
- - Other Grandchildpage

即使页面是子页面或孙子页面,显示页面层次结构的正确方法是什么?

使用以下代码:

<?php
if(!$post->post_parent){
    // get the child of the top-level page
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
}else{
    // get the child pages if we are on the first page of the child level.
    //$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");

    if($post->ancestors)
    {
        // now get an ID of the first page
        // wordpress collects ID in reverse order, so the "first" will be the last page
        $ancestors = end($post->ancestors);
        $children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
    }
}

if ($children) { ?>
    <ul>
        <?php echo $children; ?>
    </ul>
<?php } ?>

<?php
if(!$post->post_parent){
    // get the child of the top-level page
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
}else{
    // get the child pages if we are on the first page of the child level.
    //$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");

    if($post->ancestors)
    {
        // now get an ID of the first page
        // wordpress collects ID in reverse order, so the "first" will be the last page
        $ancestors = end($post->ancestors);
        $children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
    }
}

if ($children) { ?>
    <ul>
        <?php echo $children; ?>
    </ul>
<?php } ?>