How to Create a Page that Displays Random Posts

The dynamic ability of WordPress is one feature that impresses the most.
To create a page that displays random posts, create a custom page template and post the code below where you want the random posts to display:

<?php
query_posts(array(‘orderby’ => ‘rand’, ‘showposts’ => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<h1><a href=”<?php the_permalink() ?>”><?php the_title(); ?></a></h1>
<?php the_content(); ?>
<?php endwhile;
endif; ?>

What this does:
‘orderby’ => ‘rand’ – Load these posts in a random order.
‘showposts’ => 1 – Only show 1 post at a time.

You can change the number to what you want but most people tend to keep the number low.

Leave a comment

You must be logged in to post a comment.