It looks to me like theme_comment_wrapper has no way of knowing how many comments there are. Is there a good way to get this information into the comments header?

This is the evil, fragile method I'm using now: counting strings!

function phptemplate_comment_wrapper($content) {
  if (!empty($content)) {
  
  	  preg_match_all ('/<div class="comment-inner/', $content, $matches);
  	  $num_comments = count($matches[0]);
  	  
  	  $header = 'Comments ('. format_plural($num_comments, '1', '@count') . ')';
  
      return '<div id="comments"> <h5>'. $header .'</h5>'. $content .'</div>';
  } else {
      return '';
  }
}

Please tell me there's a better way.

Comments

Littlebob’s picture

I would like to know too. Anyone have a suggestion?

Vergilius’s picture

If you mean "comments per node" use this (drupal 6):

echo $node->comment_count;

chellman’s picture

I was asking this about Drupal 5, but it's good to know this exists in Drupal 6.

Vergilius’s picture

I downloaded Drupal 5 and check it. $node->comment_count exists. Try it and u ll be happy :)
Change this:

$header = 'Comments ('. format_plural($num_comments, '1', '@count') . ')';

to this:

$header = 'Comments ('. format_plural($node->comment_count, '1', '@count') . ')';

and erase your preg_match_all function.

chellman’s picture

Thanks for looking at that. I will look into this way of doing. It's not quite as simple as you say, though, because the function where this appears, theme_comment_wrapper, doesn't have access to the $node object. Making $node global might work, although that feels icky.

pcambra’s picture

I used the original solution proposed as in my site $node and $vars are empty when i use phptemplate_comment_wrapper or zen_comment_wrapper in themplate.php