您可以使用两个查询,第一个查询可以获得如下分类:
$categories = get_categories();
有关参数的更多信息
here.然后浏览您的类别集合,并获得其前n篇帖子,如下所示:
foreach($categories as $cat)
{
//get the $n posts from this category
$post_query = new WP_Query(
array(\'post_type\' => \'post\',
\'posts_per_page\' => $n ,
\'tax_query\' => array(
array(
\'taxonomy\' => $cat->taxonomy,
\'field\' => \'slug\',
\'terms\' => $cat->term_id,
)
)
)
);
echo \'<h2>\'.$cat->name.\'</h2>\';
while($post_query->have_posts()){
$post_query->the_post();
echo get_the_title();
}wp_reset_query();
}
希望能有所帮助=)