@死亡医生感谢你的帮助。代码当然有效。我还想改变第一组的风格,所以我增加了帖子数量。以下是完整代码:
<?php
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => -1,
\'order\' => \'DESC\'
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()) :
$last_month = null;
$postCount = 1;
while ( $the_query->have_posts() ) : $postCount++;
$the_query->the_post();
$the_month = get_the_time( \'Ym\' );
if ( $last_month !== $the_month ) {
if ( $last_month !== null ) {
echo \'</div>\';
}
?>
<?php if($postCount == 2) { ?>
<div class="firstgroup"> // style first group of posts
<?php } else { ?>
<div class="othergroups"> // style other groups of posts
<?php } ?>
<?php
}
$last_month = $the_month;
?>
<div>
<?php the_title(); ?>
</div>
<?php
if ( $the_query->current_post + 1 === $the_query->post_count ) {
// Last item, always close the div
echo \'</div>\';
}
endwhile;
endif;
wp_reset_postdata();
?>