New Addition – Random Posts

Random Posts

Random Posts

A few weeks ago, my friend Santhosh posted a nice little tutorial on noob2geek on how to display random posts on your wordpress blog. I was thinking about implementing it on this blog for a long time, finally I got some free time and I have implemented the feature here on this blog.

I have made a few changes to the code given on that post on noob2geek. I wanted to display random posts at the bottom of every individual post. The method on n2g does 2 things, 1 is that it displays even the current post in the random posts. Secondly, it does not work well in-line to a post as we don’t store the $post variable anywhere. Hence, the code with the changes is given below.

<!-- ************ Random Posts **************** -->
 
<h2>Other Posts You Might Like</h2>
<ul>
<?php
global $post;
$tmp_post = $post; 
$rand_posts = get_posts('exclude='.get_the_ID().'&numberposts=4&orderby=rand');
foreach( $rand_posts as $post ) :
?>
<li><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a></li>
<?php endforeach; ?>
<?php $post = $tmp_post; ?>
</ul>
 
<!-- *********** Random Posts ************** -->

Hmm… The code looks a bit weird in blockquotes. I might have to change the way a block quote is displayed. Right now, check the wp-syntax version. Other’s are not working, unfortunately.

Other Posts You Might Like

This entry was posted on Friday, March 5th, 2010 at 20:24 and is filed under Announcement. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

3 Responses to “New Addition – Random Posts”

Ratnadeep March 5th, 2010 at 20:46

nice one..
anything such for blogger blog ??

Aditya March 5th, 2010 at 21:25

No idea mate. Stopped using Blogger LONG time ago.

Aniket Zamwar March 5th, 2010 at 23:24

Dude.. im really very much impressed with ur blog.. :D

Leave a Reply