I have several authors on a blog, and on the home page (index.php) I want to query the posts so that 开发者_开发问答it only shows the latest post from each author. So say I have 5 authors it will show 5 posts each being the most recent for each author.
Any ideas on how to do this? I don't want to create separate loops for each author, it has to be automatic and using the default loop. Thanks.
You could do something like this, using a custom query (as explained here) to get the initial IDs of the latest post from each author, then add those post ids to the regular Loop query:
<?php
$querystr = "
SELECT wposts.id, max(wposts.post_date) latest_post
FROM $wpdb->posts wposts
GROUP BY wposts.post_date
";
$post_ids = array();
if($pageposts = $wpdb->get_results($querystr, OBJECT)( {
foreach ($pageposts as $post) {
$post_ids[] = $post->id;
}
}
query_posts(array('post__in' => $post_ids));
if ( have_posts() ) : while ( have_posts() ) : the_post();
echo "your loop item output here";
endwhile; else:
endif;
wp_reset_query();
?>
It's not tested though, so YMMV, but something along these lines should get the job done :P. Good luck!
精彩评论