Home > Tools > CMS > Drupal > Snippets > Drupal Block Snippet: Recent Comments by

Drupal Block Snippet: Recent Comments by

The Drupal core comment module comes with a block called Recent Comments, that displays links to the latest comments, the comment author's name and the time that passed since the comment was posted.

If you configure the comment module so that users can enter their homepage URL the comment author's name displayed in the comment block is not a link to the URL entered.

Considering the huge amount of spam comments blogs receive this seems perfectly reasonably. But you can configure Drupal so that comments by anonymous user's must be approved. This way you can prevent spam comments from being displayed publicly.

Obviously good comments add value to your site so you can give something back to the comment author if you link his name to the entered homepage URL in a recent comments block without using the nofollow attribute.

The Drupal block snippet below displays such a block with backlinks to the author's homepage with the author name restricted to a maximum length of 15 characters.

<?php
global $user;
$sql = 'SELECT u.uid, c.name, c.nid, c.subject, c.cid, c.timestamp, c.homepage, n.title FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid INNER JOIN {users} u ON c.uid = u.uid WHERE n.status = 1 AND c.status = %d ORDER BY c.timestamp DESC';
$result = db_query_range(db_rewrite_sql($sql, 'c'), COMMENT_PUBLISHED, 0, 5);
$items = array();
while (
$comment = db_fetch_object($result)) {
 
$name = $comment->name;
  if (
strlen($name) > 15) {
   
$name = substr($name,0,14);
  }
  if (
$comment->uid > 0 && user_access('access user profiles', $user)) {
   
$author = l($name, 'user/'. $comment->uid);
  } else if (
$comment->homepage) {
   
$author = l($name, $comment->homepage, array(), NULL, NULL, TRUE, FALSE);
  } else {
   
$author = $name;
  }
 
$items[] = l($comment->subject, 'node/'. $comment->nid, NULL, NULL, 'comment-'. $comment->cid, array('title' => $comment->title)) .t(' by '). $author;
}
return
theme('item_list', $items);
?>

This block is used on SEO Expert Blog. Feel free to use the code on your Drupal site.

Great little snippet of Drupal code for comments. I may install this to see if it increases comment activity. We currently have comments displaying on the middle right sidebar of the site and they display on every page. I may reduce the pages the comments display on if we install this snippet you provided above though. I don't think site-wide outbound links to commentors is the way we want to go. :) Thanks!
Thank you too for your appreciation!
We are planning to install a blogging feature on our website, but we have observed that most of the scripts are written in PHP whereas our website is entirely in ASP. We tried installing some "free asp" based scripts but it wasnt upto the mark. Do let us know if there is any ASP version of Drupal?. rgds David, Search Engine Optimization Hawk
I don't know about any ASP solution similar to Drupal but maybe someone else?
I had my own comments block created too, but wasn't sure how to link back to the author's website in the block - thanks! Any idea how I can show a "Posted XXX time ago" string of text as well?

You can, for example, use the following code to have the time interval between now and the comment being posted printed on the next line.

<?php $items[] = l($comment->subject, 'node/'. $comment->nid, NULL, NULL, 'comment-'. $comment->cid, array('title' => $comment->title)) .t(' by '). $author . '
' .t('Posted @time ago', array('@time' => format_interval(time() - $comment->timestamp)); ?>

This is done similar to Drupal's core comment block theme function implementation.

The comment_get_recent() function doesn't return information on the user.

thanks! this was very helpful!
you are welcome

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <br> <img> <h2> <h3> <h4> <h5>

More information about formatting options

Loading