List Recent Posts on Page or Sidebar – WordPress

Want to list recent posts on a page? Use the code below.

To use it in your sidebar, just make sure your wrap the entire chunk of code in <li class="widget"></li> tags so that it is recognized as an element of the sidebar <ul>. To add a title in the sidebar, wrap it in <h3 class="widget-title">Recent Posts</h3>, where “Recent Posts” is your desired title.

<?php query_posts('showposts=5'); ?>
<ul>
    <?php while (have_posts()) : the_post(); ?>
        <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    <?php endwhile;?>
</ul>

Questions? Drop ‘em in the comments

Posted April 4th, 2010 in Code Snippets.

Leave a response: