从@Ryan的帖子中可以看出,关键是设置post_parent=0
和post_type=\'page\'
.
您始终可以查看WP\\U查询对象的SQL请求,以查看需要添加哪些参数才能获得所需的结果。
此代码适用于我:
<?php
$args=array(\'post_parent\' => 0, // required
\'post_type\' => \'page\', // required
\'orderby\' => \'menu_order\', // to display according to hierarchy
\'order\' => \'ASC\', // to display according to hierarchy
\'posts_per_page\' => -1, // to display all because default is 10
);
$query = new \\WP_Query( $args );
/* Uncomment to see the resulting SQL to debug
echo $query->request; die();
//*/
if ( $query->have_posts() ) {
while($query->have_posts()) {
$query->the_post();
$post_id=get_the_ID();
$post=get_post($post_id,\'ARRAY_A\');
echo $post[\'ID\'].\': \'.$post[\'post_title\'].\'<br>\';
}
}