I have had the bright idea of changing my user permissions so as guests cannot see the comments on a node. I have no problems with Views around my site, as they can clearly see there are comments on nodes, and they will be more inclined to register. The problem seems to be when they arrive on my site directly to a node, they don't see any comment, and think "that's a bit crap; let's go."

Is there a way I could show the amount of comments below a node?

link|improve this question

77% accept rate
feedback

2 Answers

up vote 3 down vote accepted

I recommend you use template_preprocess_node(). A basic example in D6 is below, you can customize it to your liking. In your template.php file located in your theme directory, add something along the lines of (replacing YOURTHEME with the name of your theme):

function YOURTHEME_preprocess_node(&$variables) {
  $nid = $variables['node']->nid;
  $variables['num_comments'] = db_result(db_query('SELECT COUNT(cid) AS count FROM {comments} WHERE nid = %d', $nid)) . ' comment(s) on this node';
}

and save the file. Now in node.tpl.php (or any equivalent template, node-mycontenttype.tpl.php, etc) simply add:

<?php print $num_comments; ?>

Wherever you would like the comment count to be located and save. Clear the cache and then view your changes.

link|improve this answer
Thanks Laxman works a treat. Just what i was after :) – Reg Gordon Aug 6 '11 at 2:12
feedback

You can use $comment_count in node.tpl.php.

$type: Node type, i.e. story, page, blog, etc.
$comment_count: Number of comments attached to the node.

link|improve this answer
I suppose that $comment_count counts the number of comments visible to the user; if the current user cannot see comments, that variable would be set to zero. – kiamlaluno Mar 8 at 6:52
feedback

protected by kiamlaluno Mar 8 at 6:53

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

Not the answer you're looking for? Browse other questions tagged or ask your own question.