好的,您可以使用foreach循环返回每个类别的最新帖子。以下是您应该如何做到这一点。
<?php
$postids = array();
$catids = array( 1, 2, 3 ); // add category ids here.
foreach ( $catids as $catid ) {
$my_query = new WP_Query( array( \'cat\' => $catid, \'ignore_sticky_posts\' => 1, \'posts_per_page\' => 1, \'no_found_rows\' => true, \'update_post_term_cache\' => false, \'update_post_meta_cache\' => false ) );
if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post();
$postids[] = $post->ID;
endwhile;
endif;
wp_reset_postdata();
}
print_r( $postids ); // printing the array.
?>
这实际上与运行3个不同的循环相同,但现在代码更干净了。