Comment Counts in Twig

6 June, 2023

Views makes it easy to include a node's comment count as a field, but what about when you aren't using fields...when you display content, instead?

You could write a preprocess function, but why code when you don't have to? Use twig!

The comment class includes a method count(), but that method is not exposed to twig. Luckily, an alternative is:

{{ node.comment.comment_count }}

That's all there is to it!

Login or Register to Comment!