Create a page to display a random post

October 22, 2008 · Print This Article

Your blog have probably a lot of posts that your readers haven’t read yet. Why not creating a page and display a random post on it? Here’s an easy way to do it.

The first thing to do is, of course, to create a page template.

Once done, paste the following code in your new page template:
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
// WordPress loop, your random post will appear here
endwhile;
endif; ?>

That’s it! Wonderful way to give a second life to old posts, isn’t it?


Your choice for site templates and wordpress themes

Comments

Got something to say?

You must be logged in to post a comment.