2014-03-05 39 views
1

我试着让下面的函数输出数组中的所有子页面。一切工作正常期望在ASC中下订单。Wordpress获得儿童页面的升序

$my_wp_query->query(array('post_type' => 'page')); // This gives me everything 

$my_wp_query->query(array('post_type' => 'page', 'order' => 'ASC')); // This gives me nothing 

我是新来的WordPress和尝试谷歌一个小时,但我找不到。请帮助任何人。

感谢

function get_children_pages_by_page_title($page_title = ''){ 

$my_wp_query = new WP_Query(); 

$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'order' => 'ASC')); 



$page_title = empty($page_title) ? get_page_by_title() : $page_title; 

$portfolio = get_page_by_title($page_title); 
$portfolio_children = get_page_children($portfolio->ID, $all_wp_pages); 

return $portfolio_children; 
} 

回答

3

使用get_pages($args)试试这个,添加列'sort_column' => 'post_title'进行排序要

function get_children_pages_by_page_title($page_title = ''){ 
$page_title = empty($page_title) ? get_page_by_title() : $page_title; 
$portfolio = get_page_by_title($page_title); 
$args=array(
'child_of'=>$portfolio->ID , 
'sort_order' => 'ASC', 
'sort_column' => 'post_title', 
'post_type' => 'page', 
'post_status' => 'publish' 
); 
$portfolio_children=get_pages($args); 
return $portfolio_children; 
} 
+1

作品!!!非常感谢你。 – what

0

以防其他人的需要。 您需要订购所有页面,并且孩子将按此方式订购。

$my_wp_query = new WP_Query(); 
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'posts_per_page' => '-1', 'orderby' => 'menu_order', 'order' => 'ASC')); 

$children_of_parent = get_page_children($parent_id, $all_wp_pages); 

foreach($children_of_parent as $child) { 
    echo $child->ID 
} 
0

我不知道为什么@emilushi的答案有零upvotes。它只是工作。

下面是我用什么:

$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'posts_per_page' => '-1', 'orderby' => 'menu_order', 'order' => 'ASC')); 
       $page_parent = wp_get_post_parent_id(get_the_ID()); // Get page parent ID. Will return 0 for parent pages. 

//创建一个新的答案,因为我不能评论。

+1

这不提供一个问题的答案,应该是一个评论。请参阅[我应该何时评论?](https://stackoverflow.com/help/privileges/comment)。 一旦你有足够的[声望](https://stackoverflow.com/help/whats-reputation),你将可以在任何帖子上[评论](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提问者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 – Dwhitz

+0

我不确定我是否可以发表评论。让我看看这是否经过。 –