Post Pic

How to: Display a random post in your blog sidebar

What about giving a new life to your old blog posts? In this recipe, I’m going to show you how to easily display a random post on your blog sidebar.

To achieve this recipe, simply paste the following code on he sidebar.php file of your theme:

<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
     while (have_posts()) : the_post();
         the_title();
         the_excerpt();
     endwhile;
endif; ?>

That's all. Your sidebar will display a random post on each of your blog pages.

24 Responses

Jan 02 2009 12:52

Exactly what I need but I have 2 problems :
- there are no links to the post ;
- 2 posts are displayed in the list ;

Thanks for helping me ! :)

Jan 02 2009 19:24

Same as Maigret. I need a link to the post. It shows up as one post okay for me. Thanks!

Jan 02 2009 19:31

To create a link to the post, just use <a href=”<?php the_permalink();?>”>my link is here</a>

Jan 02 2009 21:16

Thanks, it works perfectly !

Jan 02 2009 21:49

wow! great.

could you please show me, how to create tab based options in sidebar. even how i can input this tab features in below of the posts. that be will great.

Jan 03 2009 12:43

Is it possible to add this in a text widget? :-?

Jan 04 2009 11:58

@Plurkr: There are plugins that let you add php to text widgets should look into that.

Great stuff as usual.

Jan 05 2009 16:33

Makes my sidebar disappear. Here’s the code I’m using:

Today’s random post:

‘rand’, ‘showposts’ => 1));
if (have_posts()) :
while (have_posts()) : the_post();
<a href=”">
endwhile;
endif; ?>

What am I doing wrong?

Jan 05 2009 16:42

Sorry, it looks like the php code got chewed up. Anyway, this code worked for me:

<?php $myposts2 = query_posts(array(‘orderby’ => ‘rand’, ‘showposts’ => 1));
foreach($myposts2 as $post) : ?>
<a href=”"></a>
<?php endforeach; ?>

Jan 08 2009 16:18

Thanks

Jan 12 2009 21:55

It works! But how to make link to this random post from title to be shown?

Jan 12 2009 22:52

@Hungry Forester: Please read my first comment, I have already explained it :)

Jan 14 2009 11:18

A warning for larger sites: sorting by rand() is very expensive for a database to perform as it can’t use an index. Even for smaller blogs, don’t be surprised if you see your load go up.

There are various better approaches but they’re more involved. Most blogs will find it usable enough.

Artem

Feb 15 2009 06:44

Hey, nice, this is what im looking for. but just like Maigret said, 2 posts are displayed in the list.

The first one is at the top of the page. this is where i place the code.

and the second one is at the bottom of the page. i dont place any random code there.

anyone can help

btw nice code. thanks.

Feb 27 2009 04:20

Hi, This is almost exactly what I am looking for, thanks.

How would I change it so that it shows only the single most recent post?

Jun 19 2009 07:30

Nice one man!

Aug 14 2009 20:12

I made this trick with this code:

‘rand’, ‘showposts’ => 10));
foreach($myposts as $post) :
?>
<a href=>

Sep 01 2009 16:49

That is so easy! I cannot believe I have been struggling with this for so long. Thanks.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required