Archive listing of posts by publish year (multiple years)

My original impression was that I believe that what is happening is that the Loop doesn’t get incremented until the_post runs so your && $postyear == $i check is actually looking at the previous post not the current one. You are going to have to reorganize this to get that check after the Loop is incremented by the_post.

That said, I don’t think I trust this logic at an even deeper level. You are setting $postyear before an enclosing loop ($postyear = get_the_time('Y', $post->ID);) so it isn’t even effected by what happens with the_post. That worries me.

I know you don’t want this much revision but for what it is worth I am thinking you need something more like:

 $oldyear = $postyear = get_the_time('Y', $post->ID);?>
 <h4><?php echo $postyear; ?></h4>
    <ul class="archive-posts"><?php
    while (have_posts()) {
      the_post();
      $postyear = get_the_time('Y', $post->ID);
      if ($oldyear != $postyear) {
    $oldyear = $postyear; ?>
    </ul><h4><?php echo $postyear; ?></h4><ul class="archive-posts">
    <?php
      } ?>
    <li><span class="archive-post-title">
    <a href="https://wordpress.stackexchange.com/questions/75665/<?php the_permalink(); ?>"><?php the_title(); ?>
    </a></span><span class="archive-post-date"><?php the_time(get_option('date_format')); ?></span></li><?php
    }
    echo '</ul>';
?>

I am going way, waaayy out on a limb with that. It has barely been tested but it should give you an idea.

Leave a Comment