Title: code in sidebar interrupting the main loop
Last modified: August 21, 2016

---

# code in sidebar interrupting the main loop

 *  [deepwhitesound](https://wordpress.org/support/users/deepwhitesound/)
 * (@deepwhitesound)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/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() ) {
             the_post();
             $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):
 * [http://www.pprbmb.com/wordpress/exhibition-title/](http://www.pprbmb.com/wordpress/exhibition-title/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [deepwhitesound](https://wordpress.org/support/users/deepwhitesound/)
 * (@deepwhitesound)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/code-in-sidebar-interrupting-the-main-loop/#post-3640519)
 * also – if anyone knows how to alter that sidebar code so that the list it brings
   up EXCLUDES a certain category, could you please assist in that? I have no idea
   where to put the following code to make it work:
 * `'cat=-5'`
 *  [paulwpxp](https://wordpress.org/support/users/paulwp/)
 * (@paulwp)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/code-in-sidebar-interrupting-the-main-loop/#post-3640534)
 * Avoid using `query_posts()`
 * use `WP_Query` instead
    [https://codex.wordpress.org/Class_Reference/WP_Query](https://codex.wordpress.org/Class_Reference/WP_Query)
 * and don’t forget to `wp_reset_postdata()` at the end.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘code in sidebar interrupting the main loop’ is closed to new replies.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [lists](https://wordpress.org/support/topic-tag/lists/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [sidebar](https://wordpress.org/support/topic-tag/sidebar/)
 * [single post](https://wordpress.org/support/topic-tag/single-post/)
 * [template](https://wordpress.org/support/topic-tag/template/)

 * 2 replies
 * 2 participants
 * Last reply from: [paulwpxp](https://wordpress.org/support/users/paulwp/)
 * Last activity: [13 years, 2 months ago](https://wordpress.org/support/topic/code-in-sidebar-interrupting-the-main-loop/#post-3640534)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
