2015-10-15 45 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 } ?> 
相關問題