2016-05-23 25 views
0

我的WordPress網站有這樣的網頁:獲取WordPress的所有子頁,而父母

-home 
    -about 
    -history 
-contact 
    -office1 
    -office2 
    -office3 
-solutions 
    -corporate 

菜單結構也是相同的。我怎樣才能得到只有子頁面爲一個數組,這樣我就可以得到這樣的:

-about 
-history 
-office1 
-office2 
-office3 
-corporate 

我希望所有與深度1.現在,我使用這個功能的網頁:

$pagelist = get_pages('sort_column=menu_order&sort_order=asc'); 
$pages = array(); 
foreach ($pagelist as $page) { 
    $pages[] += $page->ID; 
} 

但在這裏我也得到父頁面。我可以做一個循環來從數組中移除父頁面,但是有沒有任何wordpress解決方案可以在一個函數調用中獲得我想要的內容?

回答

0

您可以用WP_Query對象做到這一點:

$children_query = new WP_Query(array(
    'post_type'   => 'page', 
    'orderby'    => 'menu_order', 
    'order'    => 'ASC', 
    'post_parent__not_in' => array('0') 
)); 
$children_pages = array(); 
if($children_query->have_posts()){ 
    while($children_query->have_posts()){ 
     $children_query->the_post(); 
     $children_pages[] = get_the_ID(); 
    } 
    wp_reset_postdata(); 
} 
var_dump($children_pages); 
相關問題