Home > Tools > CMS > Drupal > Drupal Hacks > 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.

SEO Tools to Help Your Rank Higher
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
Whats the code for the "number of comments" that you used in your node-type.tpl.php file? I've been looking for it, and it would be much appreciate if you could share it. I see you bypassed $links, and I would love to do the same. Thanks!

Add the following code to your template.php:

<?php
function THEME_NAME_preprocess_node(&$variables) {
 
$node = $variables['node'];
 
// Separate out comment link.
 
if (isset($node->links['comment_comments'])) {
   
$variables['comment_link'] = l($node->links['comment_comments']['title'],
   
"node/$node->nid", array('rel' => 'nofollow',
   
'title' => t('Comments on post.')),
   
NULL, 'commentblock');
  }
  elseif (isset(
$node->links['comment_add'])) {
   
$variables['comment_link'] = l(t('Your opinion'),
   
"comment/reply/$node->nid", array('rel' => 'nofollow',
   
'title' => t('Be the first to comment.')), NULL, 'comment-form');
  }
 
// Kill comment links that are already added above.
 
if (isset($node->links)) {
   
$links = $node->links;
    if (isset(
$links['comment_comments'])) {
      unset(
$links['comment_comments']);
    }
    if (isset(
$links['comment_add'])) {
      unset(
$links['comment_add']);
    }
   
$variables['links'] = theme('links', $links, array('class' => 'links inline'));
  }
}
?>

Then you can add the comment link in your node.tpl.php like that:

<?php if($comment_link) { ?>
  <span class="comment-link"><?php print $comment_link; ?></span>
<?php } ?>
What is happening in the template.php file? BTW, can you confirm the code is correct in the last part? The snippet that goes into node.tpl.

In template.php the variable $comment_link is made available to node templates, e.g. node.tpl.php, and the comment links contained in the $links variable are unset to not show the comment links twice. The code that goes into node.tpl.php is copied from my own theme. Looks like it's working ;)

Tried this a few times, copied line for line. Of course I changed the theme name in the template file as well. Not sure what's the issue... oh well. Breaking up the links variable gave me [comment_comments] <?php print_r($node->links)?>
Here is what I did .. <?php if ($page == 0): ?> <span class="comments"> <a href="<?php print $node_url ?>" title="Permalink"><?php print $comment_count.' Comment'; if ($comment_count != 1) print 's'; ?></a> </span> <?php endif; ?>

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

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.