Comment Counts in Twig

25 April, 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

    /themes/custom/tac/images/tag-icons/d9-logo.svg
    Drupal 9
    Drupal 9

    D8

    /themes/custom/tac/images/tag-icons/d8-logo.svg
    Drupal 8
    Drupal 8

Login or Register to Comment!