WordPress: Display a Random Comment from a Post? (For Advance WP Users)

If you want to include a random comment from one of your posts you can use the following code in your theme:

<?php $post_id = 33;
// Put the ‘testimonials’ id here
$comments = get_comments(“post_id=$post_id&status=approve”);
if ($comments) { $ndx = mt_rand(1,sizeof($comments)) – 1;
$comment = $comments[$ndx]; }?>
<p>
<?php echo $comment->comment_content; ?>
</p>
<p>
<?php echo $comment->comment_author; ?>
</p>

Replace “33″ with the post ID you want to get your comments from. Such technique is often used to get comments from post that asks for feedback for example.

Looking for quality WordPress Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , | Posted under WordPress | RSS 2.0

Author Spotlight

TJ Marsh

TJ Marsh

My names Tj Marsh. I spend about 16 hours a day on the internet. Running my company and helping out others. I live in the good ole Arizona where it gets ridiculously hot. I love WordPress and a lot of other open source software along with coding.

Leave a Reply

Your email address will not be published. Required fields are marked *


8 + 7 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>