<?php
$year = '';
if (have_posts()) : while (have_posts()) : the_post();
$current_year = get_the_date( 'Y' );
if ( $year != $current_year ) echo '<h4>---' . $current_year . '---</h4>';
?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
</article>
<?php
$year = $current_year;
endwhile; endif; ?>
$year = '';
while ( .... ) :
$current_year = get_the_date( 'Y' );
if ( $year != $current_year ) {
echo '<h4>---' . $current_year . '---</h4>';
// .....
}
$year = $current_year;
endwhile;
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
</article>
<?php endwhile; endif; ?>