用一个简单的查询不可能得到每个类别一篇文章,甚至一个复杂的查询也需要比3个单独的查询更多的时间。所以,如果你想要最简单的,那么这就是解决方案-
$cats = array(\'lifestyle\', \'fashion\', \'beauty\');
$exclude_posts = array();
foreach( $cats as $cat )
{
// build query argument
$query_args = array(
\'category_name\' => $cat,
\'showposts\' => 1,
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'orderby\' => \'date\',
\'order\' => \'DESC\'
);
// exclude post that already have been fetched
// this would be useful if multiple category is assigned for same post
if( !empty($exclude_posts) )
$query_args[\'post__not_in\'] = $exclude_posts;
// do query
$query = new WP_Query( $query_args );
// check if query have any post
if ( $query->have_posts() ) {
// start loop
while ( $query->have_posts() ) {
// set post global
$query->the_post();
// add current post id to exclusion array
$exclude_posts[] = get_the_ID();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
}