$node->comment_count is returning the value of 1 in a blog post that has no comments. It returns 1 in two of my posts, both of which are the first one listed on the page. That is, there are two pages of blog teasers, and the first one on each page says "1 comment" even though one of those two posts has a comment and the other does not.

This happens regardless of whether or not I use the views module to display the teasers.

I'm printing $node->comment_count in my node-blog-teaser.tpl.php file.

I'm using CCK 6.x-2.1. The same problem existed in CCK 6.x-2.0.

I just tried adding a test comment to a third post -- one that is not currently the first one on any page -- and doing so didn't affect the other comment counts. In other words, after adding a comment to a third post, I see "1 comment" on three of my posts, two of which have a comment and one of which does not.

This is the relevant code in my node-blog-teaster.tpl.php:

<div style="clear:both;"><ul class="links inline"><li class="blog_usernames_blog first"><a href="<?php print $node->links['blog_usernames_blog']['href'] ?>" title="<?php print $node->links['blog_usernames_blog']['attributes']['title'] ?>"><?php print $node->links['blog_usernames_blog']['title'] ?></a></li>
<?php if ($node->comment_count > 0) { ?><li><a href="/node/<?php print $node->nid ?>#comment-1"><?php print $node->comment_count ?> comment<?php if ($node->comment_count > 1) { ?>s<?php } ?></a></li><?php } ?>
<li class="comment_add"><a href="/node/<?php print $node->nid ?>#comment-form" title="Add a new comment to this page.">Add new comment</a></li>
<li class="node_read_more last"><a href="/node/<?php print $node->nid ?>" title="<?php print $node->links['node_read_more']['attributes']['title'] ?>">Read more &raquo;</a></li>
</ul></div>

Comments

webcomm’s picture

Some more info... I deleted all my comments at /admin/content/comment but still saw "1 comment" in a blog teaser where there are no comments. So I had a look a the database and saw that the comment_statistics table showed 1 comment for that blog post. I edited that db record and now the blog teaser properly shows no comment count.

All this is to say that the problem is not with the ouput. The output was correct, given what was in the database. The problem must have to do with whatever caused the incorrect value in the database.

Tom Ash’s picture

I'm always getting 0 when I print it myself.

mdeltito’s picture

$node->comment_count seems to always be set to 0 for me as well.

jeff.k’s picture

Anyone find a fix for this? I too get 0 as the comment count even when there are comments.

xaris.tsimpouris’s picture

$node->comment_count is refeering in table node.comment_counts (which for me is always 0 even for posts with comments) or in table node_comment_statistics.comment_count which is correctly updated?

sutharsan’s picture

Version: 6.3 » 6.x-dev
Component: node system » comment.module

Duplicated by #1171568: Notice: Undefined property: stdClass::$comment_count in comment_node_page_additions() (line 721.
It is not yet clear if this problem still exists in Drupal 7. Marking it for version 6.x-dev for the time being.

madmanmax’s picture

The same for me: $node->comment_count = 0. Just an observation, make sure you're not outputting $node->comment which has 3 possible values:

  • 0 - comments disabled
  • 1 - no more comments allowed on the current node
  • 2 - comments can posted and viewed

See this for some details about the node object: http://drupal.org/node/49768

It's strange because node_comment_statistics seems to be updated correctly.

madmanmax’s picture

Well it seems I was making a bit of a confusion. If you are using OG make sure you're passing the right node ID or the correct $node object. I was passing to my template the group node which of course had 0 comments. So make sure you're loading/getting the right node, especially if you're getting the same value, regardless of how many comments you have posted on that node.

Status: Active » Closed (outdated)

Automatically closed because Drupal 6 is no longer supported. If the issue verifiably applies to later versions, please reopen with details and update the version.