您需要添加一个if语句current循环来打印文章周围的年份容器。
<?php if( have_posts() ) : ?>
<?php $current_year = date(\'Y\'); // Get the current year and set it
$first = true; // Set a $first variable;
while ( have_posts() ) : the_post();
if ( $first || get_the_date(\'Y\') != $current_year ) :
if ( !$first ) :
// Not first, so close previous year container
echo \'</div><!-- .year-\' . $current_year . \' -->\';
else :
// Is first, so set $first to false
$first = false;
endif;
// Set the new $current_year
$current_year = get_the_date(\'Y\');
// Print out new year container
echo \'<div class="year year-\' . $current_year . \'">\' .
\'<div class="year-header">\' . $current_year . \'</div>\';
endif; ?>
<article>
{{ Article Content Goes Here }}
</article>
<?php endwhile; ?>
<?php // Close the last year container created by our while loop.
echo \'</div><!-- .year-\' . $current_year . \' -->\'; ?>
<?php else : ?>
<?php endif; ?>
{{ Pagination Goes Here }}
只需记住将代码中的注释替换为实际的文章&;使用实际代码分页。