title: List Wordpress categories in sidebar with image
body: <p>I'm to list categories with images in the sidebar this is how I do it (and it works) I do this because I've certain categories I do not want to display!</p>
  
  <pre><code>&lt;?php $latests = new WP_Query('posts_per_page=2&amp;ignore_sticky_posts=1&amp;cat=12'); ?&gt;
  &lt;?php echo get_cat_name(12); ?&gt;
  &lt;?php while ($latests-&gt;have_posts()) : $latests-&gt;the_post(); ?&gt;
  &lt;?php if ( has_post_thumbnail() ) { the_post_thumbnail('sidebarcat'); } ?&gt;
  &lt;?php the_title(); ?&gt;
  &lt;?php endwhile; wp_reset_postdata(); ?&gt;
  </code></pre>
  
  <p>but I need to copy past that code for every single category... and all this code for only changing a number is a good practice I guess. Is there another way it could be done?</p>
  
  <p>I've tried with a foreach but it seems to be wrong </p>
  
  <pre><code>&lt;?php $latests = new WP_Query('posts_per_page=2&amp;ignore_sticky_posts=1&amp;cat=12'); ?&gt;
  &lt;?php foreach($latests as $latest) :?&gt;
  
      &lt;?php while ($latests-&gt;have_posts()) : $latests-&gt;the_post(); ?&gt;
  &lt;?php if ( has_post_thumbnail() ) { the_post_thumbnail('sidebarcat'); } ?&gt;
  &lt;?php the_title(); ?&gt;
  
      &lt;?php endwhile; wp_reset_postdata(); ?&gt;
  &lt;?php endforeach; ?&gt;
  </code></pre>