Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-31T15:14:16.373Z
Answers.Text: Nice! Could well come in handy :)
Answers.UserDisplayName: Dan Owen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-30T18:44:21.433Z
Answers.LastActivityDate: 2015-01-30T18:44:21.433Z
Answers.UserDisplayName: Seán O'Grady
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-29T20:28:03.113Z
Answers.Text: Thank you carlcs! You are like a machine :^)
  Much appreciated!
Answers.UserDisplayName: Dan Owen
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-18T08:16:05.087Z
Answers.Text: Is there a way to avoid the n+1 query in this situation?
Answers.UserDisplayName: aaandre
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2015-01-29T20:00:07.633Z
Answers.LastActivityDate: 2021-07-08T17:28:14.193Z
Answers.LastEditDate: 2021-07-08T17:28:14.193Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Tags: <entries><categories>
Title: Listing categories with post count in a nav?
ThreadText: # 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](https://craftcms.stackexchange.com/questions/394/listing-categories-alphabetically-total-post-count) 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](http://buildwithcraft.com/help/entries-related-to-category) section of the craft site:
  
  ```
  <ul>
  {% nav category in craft.categories.group('articleCategories') %}
      <li>
          <a href="{{ category.url }}">{{ category.title }}</a>
          {% ifchildren %}
              <ul>
                  {% children %}
              </ul>
          {% endifchildren %}
      </li>
  {% endnav %}
  </ul>
  
  ```
  
  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()`](https://craftcms.com/docs/3.x/element-queries.html#count) with an [`craft.entries` Element Query](https://craftcms.com/docs/3.x/element-queries.html), to only count the entries that are related to the category you're looping. Pass the category object to the [`relatedTo`](https://craftcms.com/docs/3.x/relations.html#the-relatedto-parameter) 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() %}
  
      <li class="categories__item"><a href="{{ category.url }}">{{ category.title }}</a></li>
  
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Nice! Could well come in handy :)  
  
  ---
  
  