15

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?

5 Answers 5

10

I recommend you use template_preprocess_node().
This is a basic example for D8

function YOUR_THEME_preprocess_node(&$variables) {
  $variables['comment_count'] = $variables['node']->get('YOUR_COMMENT_FIELD')->comment_count;
}

And then you can use it in your node.html.twig file like so:

{{ comment_count }}

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.

2
  • I have tried to adapt your code for the template_preprocess_node to drupal 7 but I am getting this error Notice: Undefined variable: num_comments in include() (line 116
    – Alexander
    Commented Dec 6, 2012 at 10:21
  • What have you changed while adapting? Does your database query look like the one given by @doterobcn below?
    – Laxman13
    Commented Dec 6, 2012 at 14:00
12

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.

2
  • 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.
    – avpaderno
    Commented Mar 8, 2012 at 6:52
  • 1
    Yes. And more directly $node->comment_count Commented Aug 19, 2013 at 12:39
4

Update, for Drupal 7 your query on template.php should look like:

$vars['num_comments'] = db_query("SELECT COUNT(cid) AS count FROM {comment} WHERE nid =:nid",array(":nid"=>$vars['nid']))->fetchField();
1

In Drupal 8 use this directly in your node template:

{{ node.comment.comment_count }}

Note that for it to work you need to have the default comment field in your node which is always named comment.

0

For Drupal 8:

function YOURTHEME_preprocess_node(&$variables) {
  $nid = $variables['node']->nid->value;
  $num_comment = db_query('SELECT comment_count FROM {comment_entity_statistics} WHERE entity_id = ' . $nid)->fetchAssoc();
  $variables['comment_count'] = $num_comment['comment_count'];
}

Now in page.html.twig:

{{ comment_count }}

Clear the cache and then view your changes.

2
  • It is really bad practive yo query the db like that for something that can be doine a lot simpler using the Drupal API: $variables['node']->get('comment')->comment_count;
    – ipwa
    Commented Oct 13, 2017 at 22:57
  • @ipwa if you know the comment field name, otherwise it's not a solution Commented Sep 10, 2019 at 8:21

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