Support » Themes and Templates » code in sidebar interrupting the main loop

  • i’ve inserted some code into my sidebar to bring up a list of all posts, sorted by year. the problem is that it interrupts my main loop – when clicking on both single posts as well as category links, it simply shows all posts. i understand why this is – i’ve started a loop in the sidebar which interferes with the main loop. here is the code i’ve inserted into the sidebar:

    <?php query_posts(array('nopaging' => 1, /* we want all posts, so disable paging. Order by date is default */));
    $prev_year = null;
    if ( have_posts() ) {
       while ( have_posts() ) {
          $this_year = get_the_date('Y');
          if ($prev_year != $this_year) {
              // Year boundary
              if (!is_null($prev_year)) {
                 // A list is already open, close it first
                 echo '</ul>';
              echo '<b>' . $this_year . '</b>';
              echo '<ul>';
          echo '<li><a href="'. get_permalink() .'">' . get_the_title() .'</a></li>';
          $prev_year = $this_year;
       echo '</ul>';
    } ?>

    The problem is, I don’t know how to end this loop without getting a syntax error?

    Here is a link to a single post, which you can see does not return just the post but every post (so far there are only two posts):

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘code in sidebar interrupting the main loop’ is closed to new replies.