2015-11-06 84 views
0

我正在處理一個主題,並且被卡在菜單上。該網站有幾個子頁面,這些頁面有孩子。我想實現的是當你點擊一個子頁面時,它只顯示一個帶有子頁面和子元素的菜單。僅顯示一個子頁面及其子元素

首頁
關於
活動
- 事件1
- 事件2
--Venue
--Food
--Music
- 事件3
聯繫

我想要的是當我點擊事件2時,它僅顯示事件2及其子女,而不顯示事件2的兄弟姐妹。

我目前的解決方案顯示所有的子頁面,即事件1,事件3。請在下面找到我的代碼。

<?php 
// find parent of current page 
if ($post->post_parent) { 
    $ancestors = get_post_ancestors($post->ID); 
    $parent = $ancestors[count($ancestors) - 1]; 
} else { 
    $parent = $post->ID; 
} 

$children = wp_list_pages("title_li=&depth=2&child_of=" . $parent . "&echo=0"); 

if ($children) { ?> 

    <ul class="list-children"> 
     <?php echo $children; ?> 
    </ul> 

<?php } ?> 

回答

0

您不應該取父母。如果你想得到「事件2」的孩子,那麼它將是父母 - 你不想首先獲取父母。您還想將depth設置爲1以返回1級別。

// assuming that $post == "Event 2" 
$args = array(
    'child_of' => $post->ID, 
    'post_status' => 'publish' 
    'depth'  => 1, 
    'title_li' => '' 
    'echo'  => false 
) 
$children = wp_list_pages($args); 
相關問題