Sign Up For Web Hosting Now!
WordPress Services
No.1 Wordpress Host
  • Free Domain name
  • Unlimited space
  • Unlimited traffic
  • 99.9% Server Uptime
  • FREE WP installation
  • Wordpress themes
  • Immediate activation
Buy web hosting
Sign up now Learn more
WordPress Hosting Award
WordPress Hosting Award
Facebook Twitter Plusone

How to display a random comment from a single post

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.
Previous Next
(c) Copyright 2004-2013 SiteGround. All rights reserved
WordPress is a trademark of Free Software Foundation, Inc. SiteGround is not affiliated with or endorsed by the WordPress Project or its trademark owners.