Community Documentation

Display (x) List of The Latest Comments

Last updated April 26, 2008. Created by OpenChimp on March 28, 2006.
Edited by pwolanin, Heine, Dublin Drupaller, sepeck. Log in to edit this page.

If you're looking for a solution for how to display a list of latest comments for Drupal 5.x and up, you should install the Views module ,which comes with a view called comments_recent. You can modify the settings of that view to change how many comments get returned.

Note from the moderator: Thank you for sharing the snippet with the Drupal community. In its current state the snippet might present security risks. See Writing secure code for a background on the most common problems.

Specifically, the snippet

  • bypasses node access restrictions; you can perhaps rewrite the query and pass through db_rewrite_sql.


<?php
$max
= 10;
$query = 'SELECT nid,cid,subject FROM {comments} order by timestamp desc ';
$result = db_query_range($query, 0, $max);
$items = array ();
while (
$comment = db_fetch_object($result)) {
   
$items[] = l($comment->subject, "node/$comment->nid", NULL, NULL, "comment-$comment->cid");
}
if (
count($items)) {
    return
theme('item_list', $items);
}
?>

About this page

Drupal version
Drupal 4.7.x

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here