• Is there a plugin, or a bit of code I can use, that I can put in the sidebar that will show recent posts?

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

    (@vaughan-van-dyk)

    Hi,

    You’ll need to use the query_posts template tag in your template to get however many posts (by default it takes the latest posts) and then while loop through them to determine how to display them.

    The following code for example, gets the three latest posts (irrespective of category/tag) and outputs them as links:

    <?php query_posts('showposts=3'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <a href="<?php the_permalink() ?>"><?php the_title() ?></a><br />
    <?php endwhile; endif; ?>

    More info and variations of this very powerful tag available here.

    All the best.
    Vaughan

    Sorry for the response to this old thread, but the answer above is misleading. We’re seeing quite a few posts lately from folks who get blank pages, because the loop posts were consumed by a call to query_posts() before the call to the main loop is made. The above will work perfectly well if your sidebar is called after the post content is displayed, but will show blank pages if it’s called before the Loop.

    So, let’s just highlight this important note:

    Important note

    The query_posts function is intended to be used to modify the main page Loop only. It is not intended as a means to create secondary Loops on the page. If you want to create separate Loops outside of the main one, you should create separate WP_Query objects and use those instead. Use of query_posts on Loops other than the main one can result in your main Loop becoming incorrect and possibly displaying things that you were not expecting.

    The query_posts function overrides and replaces the main query for the page. To save your sanity, do not use it for any other purpose.

    What you need is something like what was mentioned here:

    Recent
    <ul class="recent-posts">
      <?php
        $archive_query = new WP_Query('showposts=5');
        while ($archive_query->have_posts()) : $archive_query->the_post(); ?>
      <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
      <?php endwhile; ?>
    </ul>

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Recent posts in sidebar’ is closed to new replies.