Title: Showposts Loop
Last modified: August 19, 2016

---

# Showposts Loop

 *  Resolved [chanel](https://wordpress.org/support/users/chanel/)
 * (@chanel)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/showposts-loop/)
 * I’m trying to show the 5 recent posts in my bar with this loop:
    **<?php query_posts(‘
   showposts=5’); ?>**
 * However, it changes my main index content from showing 3 blog posts to 5 posts.
 * Is there a way to show recent posts in my sidebar without it interfering with
   my main content?
 * Note: I do not want to use the “recent posts” widget because I am using my own
   alteration of the loop. Example below:
 *     ```
       <?php query_posts('showposts=5'); ?>
       <ul>
                       <?php while (have_posts()) : the_post(); ?>
                       <li class="latestpost">
       <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> <br /><span class="latestpostmeta"><?php the_time('M jS, Y') ?> | <?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?> </span></li>
                       <?php endwhile; ?>
                       </ul>
       ```
   
 * You can view it at [http://kisschanel.com/blog](http://kisschanel.com/blog), 
   located under “Recent Posts” in the sidebar.

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

 *  [ambrosite](https://wordpress.org/support/users/ambrosite/)
 * (@ambrosite)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/showposts-loop/#post-1699842)
 * Try the get_posts function, rather than query_posts:
 * [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts)
 *  Thread Starter [chanel](https://wordpress.org/support/users/chanel/)
 * (@chanel)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/showposts-loop/#post-1699844)
 * Thanks for the response ambrosite. But I tried that already and it only fetched
   3 posts, which is what’s set in my Reading settings. I need for it to be different
   than what’s showing on the main content.
 *  [ambrosite](https://wordpress.org/support/users/ambrosite/)
 * (@ambrosite)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/showposts-loop/#post-1699847)
 * In that case, you’ll need to create a custom loop using WP_Query. Something like
   this:
 *     ```
       <?php
       $recentPosts = new WP_Query();
       $recentPosts->query('showposts=5');
       while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
               <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
               <p><?php the_content(); ?></p>
       <?php endwhile; ?>
       ```
   
 *  Thread Starter [chanel](https://wordpress.org/support/users/chanel/)
 * (@chanel)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/showposts-loop/#post-1699855)
 * That didn’t work ambrosite. Here’s the scenario:
 * left side : Only supposed to show 3 blog posts in it’s entirety.
    right sidebar:
   Only supposed to show 5 listings of the recently posted blog TITLES, date, and
   comment count. Not the content.
 *  [ambrosite](https://wordpress.org/support/users/ambrosite/)
 * (@ambrosite)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/showposts-loop/#post-1699859)
 * OK, well if you want the date to display instead of the content, just replace
   the_content with the_time, like this:
 *     ```
       <?php
       $recentPosts = new WP_Query();
       $recentPosts->query('showposts=5');
       while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
               <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
               <p><?php the_time(); ?></p>
       <?php endwhile; ?>
       ```
   
 * If you paste that code into your sidebar, does it display the five posts you 
   are looking for?
 *  [ambrosite](https://wordpress.org/support/users/ambrosite/)
 * (@ambrosite)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/showposts-loop/#post-1699860)
 * Actually, building on the code snippet you posted originally, I suppose what 
   you really want is this:
 *     ```
       <?php
       $recentPosts = new WP_Query();
       $recentPosts->query('showposts=5'); ?>
       <ul>
       <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
       <li class="latestpost">
       <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> <br /><span class="latestpostmeta"><?php the_time('M jS, Y') ?> | <?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?> </span></li>
       <?php endwhile; ?>
       </ul>
       ```
   
 *  Thread Starter [chanel](https://wordpress.org/support/users/chanel/)
 * (@chanel)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/showposts-loop/#post-1699861)
 * That last submission worked **perfectly**. Thank you ambrosite!

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

The topic ‘Showposts Loop’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)
 * [sidebar](https://wordpress.org/support/topic-tag/sidebar/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 2 participants
 * Last reply from: [chanel](https://wordpress.org/support/users/chanel/)
 * Last activity: [15 years, 8 months ago](https://wordpress.org/support/topic/showposts-loop/#post-1699861)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
