for some reason, I want to create a "recent comments" block on my site

I need to do it by creating a new block, and paste some php code in there. Does anyone have a code snippet to do this?
I don't want to use the block that is available by standard...

thanks already!

Comments

sillygwailo’s picture

The Views Module now has some list views for comments, so you could try that. That means you wouldn't have to maintain PHP snippets as Drupal.

That said, here's the recent comments block that I whipped up a few days ago:

  $result = db_query("SELECT name, cid, nid FROM {comments} WHERE status = 0 ORDER BY timestamp DESC  LIMIT 10;");
  while ($c = db_fetch_object($result)) {
    $item = $c->name . " commented on ";    
    $n = db_result(db_query("SELECT title FROM {node} WHERE nid = %d", $c->nid));
    $item .= l($n, 'node/' . $c->nid, array('rel' => 'nofollow'), NULL, 'comment-' . $c->cid);
    $items[] = $item;
  }
 return theme('item_list', $items);

(Username formerly my full name, Richard Eriksson.)

jorre’s picture

Nice, thanks Richard.

I've tried the views module last week and this sure gives me some possibilities,

but: is there a way to use views in my page.tpl.php file without having to use blocks..............
some php code like "return view('viewname')"
?

Pakfriet.be
Uzegt.be

sillygwailo’s picture

I don't know if it's "the right way", but print theme_view('view name') seems to work.

(Username formerly my full name, Richard Eriksson.)

jorre’s picture

I tried your recent comments code and it's just great!

I would love to tune it a little, to have a small image displayed in front of every recent comment instead of a list item bullet in front of it. Do you know how I could achieve this when still using your code?

let's say if we use

Only local images are allowed.

Pakfriet.be
Uzegt.be

Helge’s picture

The link is not correct composite in version 4.7.4:

Comment links looks like: "node/281/175#comment-175"
So i changed the code to:

$item .= l($n, 'node/' . $c->nid, '/' . $c->cid, array('rel' => 'nofollow'), NULL, 'comment-' . $c->cid);

but it will not work.
Think there is an syntax problem.
Can anyone help?

ncameron’s picture

This might come in useful for someone. This is basically the same as above except you can select which content types you want to show comments for. Useful if you don't want comments on profiles to show up. I also added a little bit of formatting to the comments so the look like: "this is my comment...".

<?php
  $result = db_query("
  SELECT comments.subject, comments.nid, comments.cid
  FROM {comments}, {node}
  WHERE comments.nid = node.nid
  AND node.type IN ('content_type_one', 'content_type_two', 'content_etc_etc')
  ORDER BY timestamp DESC  
  LIMIT 5;");
    
  while ($c = db_fetch_object($result)) {
	
    $item = l('"'.$c->subject.'..."', 'node/' . $c->nid, array('rel' => 'nofollow'), NULL, 'comment-' . $c->cid);
    $items[] = $item;
	}
  
return theme('item_list', $items);
?>

Enjoy,
Neil

- Connecting Language Learners
www.huitalk.com