Comment Counts in Twig

18 June, 2024

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!

  • tip icon. tip
  • D9

    Drupal 9
    Drupal 9


    Drupal 8
    Drupal 8

Login or Register to Comment!