Free Wordpress Blog Setup

How to Create a Page that Displays a Random Post in WordPress

By Editorial Staff in Themes
How to Create a Page that Displays a Random Post in WordPress

We mentioned a plugin that allows you to have Stumbleupon like Browsing system in WordPress. The problem with that plugin is that it redirects a user to the actual URL which can create some user end issues such as page not loading, redirect error etc. In this post we will show you how you can create a page that displays a random post in WordPress, so you can simply send your user to that page and they can read a new post every time they refresh the page.

Step 1: of this tutorial is to create a custom page template in WordPress.

Step 2: Add the following code in your custom page template:

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

That’s it. Depending on the name that you choose for the page, you can now create a button in your navbar for users to click on that page and view a random post.

Live Example

(Simply refresh a page to see a new post)

What Next?

Digg it
Save This Page
Subscribe to WPBeginner
Stumble it
Free Wordpress Blog Setup

Comments

4 Responses to “How to Create a Page that Displays a Random Post in WordPress”
  1. Hi,

    I have followed instructions here and my random page works fine in FireFox, but the css is not rendering properly at all in IE or Chrome. Oddly enough it does render ok in Chrome on my local version (local server) but not on the live version.

    You can test it out for yourself if you go to my site and click on the Random Moments Icon in the sidebar or the menu item.

    Any thoughts?

  2. Thank you for checking that out. I thought I went over that template page carefully, but I guess not. I assume you meant the extra div being closed prematurely is on the template page I created. Must run out for the day but will go over that when I return.

    Thanks again :-)

  3. Solution:

    There wasn’t an extra closing tag for a div as indicated in validation. The openings and closings all equaled out.

    The validation check generated that error notice due to a previous issue in the page with a call for an Adsense ad block. I had commented it out –perhaps not completely — because it was still generating a closing tag for a script that was not open to begin with. That caused the validation to erroneously indicate the extra div closing.

    It took a while to track it back, but it all works fine now in any browser after deleting that commented out Adsense call line line (didn’t need it anyway). The page validates much better now with no div error remaining.

    Thanks again for your help. You steered me in the right direction.

    :-)
    John

Share Your Opinions

Tell us what you're thinking...
and if you want a pic to show with your comment, then get gravatar!

Please make sure that you have read our Comment Policy.

Due to high volume of request from our readers, we are adding this feature that allows you to stay updated with this post's comments without having to participate in the discussion even though we would love your input as always. Don't worry we hate SPAM just as much as you do, so you will never receive any SPAM messages from our site and that's our promise to you.

Subscribe without commenting

Close Bar