Display recent posts in WordPress sidebar
Here’s a bit of code to put in your sidebar to display the most recent posts on your blog:
<?php $myposts = get_posts('numberposts=10&offset=1');
foreach($myposts as $post)
>
<li><a href="<?php the_permalink(); ?>"><?php the_title();?></a></li>
<?php endforeach; ?>
You can set the number of recent posts to appear on the first line of code where it says numberposts=10 – change the 10 to the number of posts you want to appear. You can choose to offset the recent posts by any number; for example, if you select offset=1, as we did above, it won’t display the most recent post, but will start the list from the second-most recent post.
Category: Code Snippets










Just FYI, this code works with latest versions of WordPress; I’m using it on my inner pages, http://www.pixelita.com/about-us and so forth:

Substitute [ and ] for left and right carets in code below. The number “10″ can be replaced by however many posts you want displayed; I chose only one, the latest one.
[?php wp_get_archives('type=postbypost&limit=10'); ?]
Thanks!
its working!!!!!
wp_get_archives(‘title_li=&type=postbypost&limit=1′); works for displaying the title of each post, but what about showing the description of each post?
Thanks! I was looking for thist to! It’s working!!!
Hi, thanks for this.
One question: Is it possible to add a “previous posts” lik in the sidebar? And that these previous posts also appear in the sidebar after clicking the link?
Would be great!
One problem with the above code is when you call that function in your sidebar which is called before the start of the loop in single.php. Your single.php file will list down all the articles of your blog post irrespective of the one which is called.
In such cases, you can also use
<?php wp_get_archives(‘title_li=&type=postbypost&limit=10′); ?>
This will list down latest 10 posts in ul, li format. This is bit easier.