2016-03-02 35 views
0

我有一個小問題。顯示grandchilderen的父母

在wordpress中,我有一個父母,一個孩子,一個孩子,一個孩子和一個偉大的孩子作爲頁面結構。

,我使用的代碼是這樣的:

function wpb_list_child_pages_popup() { 

    global $post; 

    if (is_page() && $post->post_parent) 

    $childpages = wp_list_pages('sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0'); 
else 
    $childpages = wp_list_pages('sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0'); 

if ($childpages) { 

    $string = '<ul id="child-menu">' . $childpages . '</ul>'; 
} 

return $string; 

} 

add_shortcode('wpb_childpages_popup', 'wpb_list_child_pages_popup'); 

我只看到當前子頁面或偉大的盛大孩子的父頁。

如何及時使用此代碼以確保當前頁面顯示列表中的2位父母?

回答

0
<?php global $post; $thispage = $post->ID; // grabs the current post id from global and then assigns it to thispage ?> 
     <?php $pagekids = get_pages("child_of=".$thispage."&sort_column=menu_order"); // gets a list of page that are sub pages of the current page and assigns then to pagekids ?> 
     <?php if ($pagekids) { // if there are any values stored in pagekids and therefore the current page has subpages ?> 
      <ul> 
       <?php wp_list_pages("depth=1&title_li=&sort_column=menu_order&child_of=".$thispage); // display the sub pages of the current page only ?> 
      </ul> 
     <?php } elseif($post->post_parent) 
       $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); if ($children) { // if there are no sub pages for the current page ?> 
    <ul> 
    <?php echo $children; ?> 
    </ul> 
     <?php } ?> 
+0

謝謝,但事情是我在模板中使用簡碼。我怎麼能合併這兩個通過短代碼工作? – Dionoh

0

試試這個

function wpb_list_child_pages_popup() { 

    global $post; 
    $thispage = $post->ID; 
    $pagekids = get_pages("child_of=".$thispage."&sort_column=menu_order"); 

    if ($pagekids) { // if there are any values stored in pagekids and therefore the current page has subpages 
     echo '<ul>'; 
      wp_list_pages("depth=1&title_li=&sort_column=menu_order&child_of=".$thispage); // display the sub pages of the current page only 
     echo '</ul>'; 
    } elseif($post->post_parent){ 
     $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); 
    } 

    if ($children) { // if there are no sub pages for the current page 
     echo '<ul>'; 
     echo $children; 
     echo '</ul>'; 
    } 
} 

add_shortcode('wpb_childpages_popup', 'wpb_list_child_pages_popup');