How to Display Most Recent Posts Anywhere in WordPress

To display the most recent posts anywhere in wordpress, use the following code snippet. Paste it anywhere in wordpress template files and it will display the five most recent posts.

<?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>

To display more (or less) then five posts, just change the number 5 to the number of your choice in the first line of the code. For example, the following code will display the seven most recent posts,

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

Note that you cannot use php code in sidebar widgets or in posts/pages. You need to edit the theme template files. However, if you do need to use it in widgets or posts/pages then install the Exec-PHP plugin to make it work.


One Response to “How to Display Most Recent Posts Anywhere in WordPress”

  1. Show Random Posts Anywhere in Wordpress on November 28th, 2009

    [...] already know how to display most recent posts anywhere in wordpress. To show random posts, you can use the following code [...]

Got something to say?

Wordpress installation, upgrade, plugins and theme development are my forte. If you want to spend more time blogging and less time fiddling with your templates, plugins and technical issues, I can help. Find out more about my services, then contact me.

Wordpress Tips, Tricks & Hacks!

Make Money With Wordpress!

Try Aweber for $1 Studiopress Themes

News!