Skip to content

Devi Sri Mari Blog:: Web Technology Trends, News, and Social Media

Menu
  • About Me
  • CONTACT US
  • Sample Page
  • WEB DEVELOPMENT
Menu

How to display a random comment from a post?

Posted on January 29, 2014

wordpress1

If you want to add a random comment from one of your posts by using the following code in your theme:

<?php $post_id = 35;

$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 “35” 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

Leave a Reply Cancel reply

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

Recent Posts

  • “Who needs Superheros when there is a Real hero and she is called as Mother”
  • Improve Your Business Behind Social Media
  • Single-Page Application Benefits and How it Works
  • World Sparrow Day
  • Google’s ‘Mobilegeddon’ Will Affect Search Results
  • How To Use Google’s New Blocked Resources Report
  • Wikipedia Lists Ten Most Edited Pages Of 2014
  • Google Is Shutting Down The Google Earth API
  • Google Maps App Adds Real-Time Weather & Facts
  • Google Maps App Gets Design Revamp
©2025 Devi Sri Mari Blog:: Web Technology Trends, News, and Social Media | Design: Newspaperly WordPress Theme