假设您知道(或知道如何获得)$id
(作为父帖子的整数),使用the post_parent
parameter:
$albums = new WP_Query(array(
\'post_type\' => \'gallery\',
\'posts_per_page\' => 6,
\'post_parent\' => $id
));
根据此评论编辑:
我不知道家长的帖子。我想列出任何父页面中最近的子页面。
虽然只查询父页面很容易(通过传递\'post_parent\' => 0
), 只查询子页面并不是那么简单。
一种方法是查询所有gallery
帖子,循环浏览,并添加任何具有post_parent
转换为另一个变量;然后输出该变量。
// Query all gallery posts
$all_albums = new WP_Query( array(
\'post_type\' => \'gallery\',
\'posts_per_page\' => -1
) );
// Array to hold latest albums
$latest_child_albums = array();
// Latest album counter
$latest_albums_count = 0;
// If we already have 5, no need to keep looping
while ( 5 < $latest_albums_count ) {
// Loop through each gallery post
foreach ( $all_albums as $album ) {
// If the current post has a parent
if ( $album->post_parent != 0 ) {
// Add it to the latest albums array
$latest_child_albums[] = $album;
// Increment the counter
$latest_albums_count++;
}
}
}
// Loop through the array of latest albums
if ( $latest_child_albums->have_posts() ) : while ( $latest_child_albums->have_posts() ) : $latest_child_albums->the_post();
// Loop content here
endwhile; endif;
// Be kind; rewind.
wp_reset_postdata();