Display random posts in WordPress
jhnidk - December 8th, 2010 in: WordPress
On a blog, it’s typically only a small portion of the posts that’s being read.
However if you want to inspire your readers to read further posts, a random post script can be a effective tool to expose additional content to your users and thereby increase the stickiness of your site.
In this post you’ll learn how easy it is to insert a random post script in WordPress.
Random post script
Inserting a random post script is easy in WordPress. WordPress has strong standard functions that can easily be modified to select random posts from your content.
The PHP snippet:
query_posts(array(‘orderby’ => ‘rand’, ‘showposts’ => 1));
Selects 1 random post from the WordPress database.
The snippet can be included in a small php script the following way:
<?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; ?>
Next step is to included this functionality in your blog. This can easily be done by finding the relevant page template in the WordPress backend. If you want to display link to a random post in the single post page you should navigate to this page:
Apperence -> Editor -> single.php
And paste the script where ever you like the random post link to be displayed.
Conclusion
A link to random posts can increase the stickiness by inspiring your readers to view additional content on your blog. The script is very easy to implement, and can be styled in many ways to fit perfectly into your blog.
Recommended further reading:
![]() |
Teach Yourself Visually WordPress
Get your blog up and running with WordPressAre you itching to share your thoughts with the world through blogging but aren’t sure how? Teach Yourself VISUALLY WordPress introduces you to one of the most popular, easy-to-use blogging platforms: WordPress. This… |
![]() |
Professional WordPress (Wrox Programmer to Programmer)
An in-depth look at the internals of the WordPress system. As the most popular blogging and content management platform available today, WordPress is a powerful tool. This exciting book goes beyond the basics and delves into the heart of the… |
![]() |
Using WordPress, with DVD
WordPress has grown into the #1 blogging tool in its category: several million bloggers have downloaded this powerful open source software, and millions more are using WordPress.com’s hosted services. Thirty-two of Technorati’s Top 100 blogs now use WordPress. Using WordPress… |






Is there any way of doing this :
a button or an image which when pressed could display a random post ?
Nadeem Khan (December 28th, 2011)