<p><strong>Bonus Answer.</strong> If you want to sort by the amount of posts each category has you can do the following</p>

<pre><code>{% set allCats = craft.categories.group('articleCategories').level(1).find() %}

{% set catArray = {} %}

{% for cat in allCats %}

    {% set catArray = catArray | merge({(cat.slug):craft.entries.relatedTo(cat).total() }) %}

{% endfor %}

{% set catArray = catArray|sort|reverse %}

{% for catSlug, count in catArray %}

    {% set category = craft.categories.slug(catSlug).first() %}

    &lt;li class="categories__item"&gt;&lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;&lt;/li&gt;

{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-01-31T15:14:16.373Z
Nice! Could well come in handy :)
Dan Owen
CC BY-SA 3.0
2015-01-30T18:44:21.433Z
2015-01-30T18:44:21.433Z
Seán O'Grady
<p>Within your <code>nav</code> loop, you would use the method <a href="https://craftcms.com/docs/3.x/element-queries.html#count" rel="nofollow noreferrer"><code>count()</code></a> with an <a href="https://craftcms.com/docs/3.x/element-queries.html" rel="nofollow noreferrer"><code>craft.entries</code> Element Query</a>, to only count the entries that are related to the category you're looping. Pass the category object to the <a href="https://craftcms.com/docs/3.x/relations.html#the-relatedto-parameter" rel="nofollow noreferrer"><code>relatedTo</code></a> parameter.</p>
<pre><code>{% nav category in craft.categories.group('articleCategories') %}

    {# Get the count of entries related to `category` #}
    {% set entryCount = craft.entries.relatedTo(category).count() %}

    {{ entryCount }}

{% endnav %}
</code></pre>

CC BY-SA 3.0
2015-01-29T20:28:03.113Z
Thank you carlcs! You are like a machine :^)
Much appreciated!
Dan Owen
CC BY-SA 4.0
2021-02-18T08:16:05.087Z
Is there a way to avoid the n+1 query in this situation?
aaandre
CC BY-SA 4.0
2015-01-29T20:00:07.633Z
2021-07-08T17:28:14.193Z
2021-07-08T17:28:14.193Z
carlcs
<p>I'm trying to list my categories (and sub categories, just one sub level) as a nav, and have the number of entries in each of those categories beside each one.</p>

<p>There is another question on here which achieves a simple list <a href="https://craftcms.stackexchange.com/questions/394/listing-categories-alphabetically-total-post-count">here</a> but trying to apply that logic to what i have for my nav is scrambling my brain as everything I've tried throws an error.</p>

<p>This is what i have, taken from the <a href="http://buildwithcraft.com/help/entries-related-to-category" rel="nofollow noreferrer">Get Help</a> section of the craft site:</p>

<pre><code>&lt;ul&gt;
{% nav category in craft.categories.group('articleCategories') %}
    &lt;li&gt;
        &lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;
        {% ifchildren %}
            &lt;ul&gt;
                {% children %}
            &lt;/ul&gt;
        {% endifchildren %}
    &lt;/li&gt;
{% endnav %}
&lt;/ul&gt;
</code></pre>

<p>Any thoughts on how i can get the number of entries each category has in it, in this?</p>

<entries><categories>
Listing categories with post count in a nav?
# Listing categories with post count in a nav?

(Asked by: username_0 on 2015-01-29)

I'm trying to list my categories (and sub categories, just one sub level) as a nav, and have the number of entries in each of those categories beside each one.
There is another question on here which achieves a simple list here but trying to apply that logic to what i have for my nav is scrambling my brain as everything I've tried throws an error.
This is what i have, taken from the Get Help section of the craft site:

{% nav category in craft.categories.group('articleCategories') %}
    
        {{ category.title }}
        {% ifchildren %}
            
                {% children %}
            
        {% endifchildren %}
    
{% endnav %}


Any thoughts on how i can get the number of entries each category has in it, in this?

**Tags:** entries, categories


---

## 2 answers

### Answer by username_2 on 2015-01-29. Score: 12 | Accepted answer

Within your nav loop, you would use the method count() with an craft.entries Element Query, to only count the entries that are related to the category you're looping. Pass the category object to the relatedTo parameter.
{% nav category in craft.categories.group('articleCategories') %}

    {# Get the count of entries related to `category` #}
    {% set entryCount = craft.entries.relatedTo(category).count() %}

    {{ entryCount }}

{% endnav %}

> **Comments:**
> **username_0**: Thank you username_2! You are like a machine :^) Much appreciated!  
> **username_3**: Is there a way to avoid the n+1 query in this situation?  

---

### Answer by username_1 on 2015-01-30. Score: 3

Bonus Answer. If you want to sort by the amount of posts each category has you can do the following
{% set allCats = craft.categories.group('articleCategories').level(1).find() %}

{% set catArray = {} %}

{% for cat in allCats %}

    {% set catArray = catArray | merge({(cat.slug):craft.entries.relatedTo(cat).total() }) %}

{% endfor %}

{% set catArray = catArray|sort|reverse %}

{% for catSlug, count in catArray %}

    {% set category = craft.categories.slug(catSlug).first() %}

    {{ category.title }}

{% endfor %}

> **Comments:**
> **username_0**: Nice! Could well come in handy :)  

---

