您可以获得类别,对于每个类别,您可以使用list category posts plugin 将相关帖子回显到div中的快捷码:
$categories = get_categories();
foreach ($categories as $category) {
echo \'<
div>
\';
$cat=$category->cat_name;
echo do_shortcode( \'[catlist id=\'.$cat.\' numberposts=5 ]\');
echo \'<
/div>
\';
}
这会出现在你博客索引的“最新帖子”部分。然后,您可以设置每个div的样式以适合主题的布局(左浮动,最小宽度)
不使用任何插件,您可以通过以下方式更改代码:
$categories = get_categories();
foreach ($categories as $category) {
echo \'<
div>
\';
$cat=$category->cat_name;
$args=array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'category_name\' => $cat,
\'posts_per_page\' => 5,
\'ignore_sticky_posts\'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ( $my_query->have_posts() ) : $my_query->the_post();
echo \'<
h1><
a href=\'.the_permalink().\'>\'.the_title().\'</a></h1>
\';
endwhile;
} //endif
wp_reset_postdata();
echo \'<
/div>
\';
} //foreach