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.



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