2015-10-15 31 views
0

我的页面下面的结构,我需要有那些谁拥有子页面的页面显示的相同的结构:保持页面层次结构中wp_list_pages,即使对孩子或孙子

- 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> 

这适用于父页面。但是,当我在其中一个子页面或孙子页面上时,这不再起作用。

按照上面的结构例子,当我在 'Childpage' 我只得到如下:

- Childpage 
- Other Childpage 

当我在 'Grandchildpage' 我只得到:

- - Grandchildpage 
- - Other Grandchildpage 

甚至当页面是孩子或孙子时,显示页面层次结构的正确方法是什么?

回答

1

使用下面的代码:

<?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 } ?>