How to put a random post on your Wordpress blog

Posted on January 4th, 2007.


How to put a random post or excerpt on your Wordpress blog? That was the question, which crossed my mind yesterday. Thanks to Google, that I've found the method on Wordpress forum. It is just simple. What you need to do is paste the code below on your theme files. In this blog, I put the random excerpt on the top right, so that it is easy to be noticed.

PHP:
  1. <?php
  2. $rand_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY RAND() LIMIT 1");
  3. query_posts("p=$rand_id");
  4. ?>
  5. <?php while (have_posts()) : the_post(); ?>
  6. <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
  7.  <?php the_excerpt(); ?>
  8. <br /><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">Read more...</a>
  9. <?php endwhile; ?>

< ?php the_excerpt(); ?> can be changed to < ?php the_content(); ?> if you want to display the full post instead of the excerpt.

If you dont want to mess with this code, you have another choice to use Wordpress plugins from wasabi, Random Posts and Random Excerpts. According to the author, you can limit the length of the excerpt generated, which cannot be done by the code above.

But I don't use the plugins. Doing something without plugins is better for me because I can learn while applying the code. Not just knowing how to activate or deactivate something. But it is your choice by the way

Related links:
Old posts. Don’t throw them away. Why?
Templates Tags - Post tags at Wordpress Codex

Bookmark this post on del.icio.us:How to put a random post on your Wordpress blog digg:How to put a random post on your Wordpress blog spurl:How to put a random post on your Wordpress blog wists:How to put a random post on your Wordpress blog simpy:How to put a random post on your Wordpress blog newsvine:How to put a random post on your Wordpress blog blinklist:How to put a random post on your Wordpress blog furl:How to put a random post on your Wordpress blog reddit:How to put a random post on your Wordpress blog fark:How to put a random post on your Wordpress blog blogmarks:How to put a random post on your Wordpress blog Y!:How to put a random post on your Wordpress blog smarking:How to put a random post on your Wordpress blog magnolia:How to put a random post on your Wordpress blog segnalo:How to put a random post on your Wordpress blog

Probably related entries:

Make a Comment

Make A Comment: ( 8 so far )

blockquote and a tags work here.

8 Responses to “How to put a random post on your Wordpress blog”

RSS Feed for TechLogs Comments RSS Feed

thanks for the linkback. the random post plugin is really useful. it digg up all your old posts to in front.

CypherHackz
January 4th, 2007

Yes, it is very useful. By the way, the link to the plugin on your post is broken. This is the new link

http://wasabi.pbwiki.com/Random%20Posts%20and%20Random%20Excerpts

korokmu
January 4th, 2007

Thanks for the good link

WebTrafficMan
January 5th, 2007

Thanks for the good link. Great plugin

WebTrafficMan
January 5th, 2007

yg related post nie lak cam ne?

aman
January 7th, 2007

Use related posts plugin from Wasabi.

http://wasabi.pbwiki.com/Related%20Entries

korokmu
January 7th, 2007

We can do that in blogger? I like to know.

Prismatico
January 19th, 2007

I have googled for that, but the result is negative. Maybe you are interested to swith to wordpress.

korokmu
January 19th, 2007

Where's The Comment Form?

Sponsored

Random post

Yahoo Domain

After offering domains at $1.99/year and $2.99/year, Yahoo is now offering domain at $4.98/year up to 5 years subscription. This plan helps you to save 50% per year.
If I have a credit card...
By the way , is Visa Electron same with Visa?


Read more...

RSS

Topics

Links

Blogroll

Misc

Liked it here?
Why not subscribe to this site feed? You can get automatic updates for free! Click here to subscribe