<p>Assuming that your products are entries, and comments are related to a product through an entries field, you can use the following:</p>

<p><code>{% set comments = craft.entries.section('productcomments').relatedTo(entry).all() %}</code></p>

<p>This uses the <a href="https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#relatedto" rel="nofollow noreferrer">relatedTo</a> parameter.</p>

<p>You can then return how many comments are found by using:</p>

<pre><code>{% if comments|length %}
    &lt;p&gt;{{ comments|length }} comments&lt;/p&gt;
{% endif %}
</code></pre>

CC BY-SA 4.0
2019-02-16T11:21:49.007Z
Thank you. This works. But i always have to choose the comments manually in the product entry through an entries field? It's actually to much work just to show how many entries are made. if i could write the "count" result in an array, then i could use twig's max function. Is that possible?
TomS
CC BY-SA 4.0
2019-02-15T11:12:53.013Z
2019-02-15T11:12:53.013Z
Jamie Wade
<p>I would like to count comments that belong to a certain entry (product). I have this so far:</p>

<pre><code>{% set comments = craft.entries().section('productcomments').all() %}
 {% set counter = 0 %}
  {% for comment in comments %}
   {% if comment.title|length and comment.title == entry.title %}
    {% set counter = counter+1 %}               
    {{ counter }} comments
   {% endif %}
  {% endfor %}
</code></pre>

<p>This actually works, but it displays the results after each other (like this:1 comment 2 comment 3 comment - if i have 3 comments).
I would like to show only "3 comments". Actually display the max result.
Twig's "loop.last" doesn't work because of the condition.
Is there another way?
Thank you very much!</p>

<loop><comments><count>
Count entries with condition
# Count entries with condition

(Asked by: username_0 on 2019-02-15)

I would like to count comments that belong to a certain entry (product). I have this so far:
{% set comments = craft.entries().section('productcomments').all() %}
 {% set counter = 0 %}
  {% for comment in comments %}
   {% if comment.title|length and comment.title == entry.title %}
    {% set counter = counter+1 %}               
    {{ counter }} comments
   {% endif %}
  {% endfor %}

This actually works, but it displays the results after each other (like this:1 comment 2 comment 3 comment - if i have 3 comments).
I would like to show only "3 comments". Actually display the max result.
Twig's "loop.last" doesn't work because of the condition.
Is there another way?
Thank you very much!

**Tags:** loop, comments, count


---

## 1 answers

### Answer by username_1 on 2019-02-15. Score: 3

Assuming that your products are entries, and comments are related to a product through an entries field, you can use the following:
{% set comments = craft.entries.section('productcomments').relatedTo(entry).all() %}
This uses the relatedTo parameter.
You can then return how many comments are found by using:
{% if comments|length %}
    {{ comments|length }} comments
{% endif %}

> **Comments:**
> **username_0**: Thank you. This works. But i always have to choose the comments manually in the product entry through an entries field? It's actually to much work just to show how many entries are made. if i could write the "count" result in an array, then i could use twig's max function. Is that possible?  

---

