Answers.Body: <p>You'll want to query for the number of entries <em>inside</em> the for loop.</p>
  
  <pre><code>{% for categoryName in craft.categories.group('markets') %}
      {% set numofEntries = craft.entries.section('posts').relatedTo(categoryName).total() %}
  
      &lt;a href="{{ categoryName.url }}"&gt;{{ categoryName.title }}-{{numofEntries}}&lt;/a&gt;
  {% endfor %}
  </code></pre>
  
  <p></p> </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-18T21:55:21.197Z
Answers.LastActivityDate: 2016-09-18T21:55:21.197Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <p>If you want to display the total number of entries related to <em>each</em> category, you'll need to put the <code>{% set numofEntries ... %}</code> statement inside your <code>for</code> loop. You'll also need to change the <code>relatedTo(category)</code> part to <code>relatedTo(categoryName)</code> (or change the <code>categoryName</code> to <code>category</code>, which would probably be more appropriate).</p>
  
  <p>Something like this should work:</p>
  
  <pre><code>{% set numofEntries = craft.entries.section('posts').relatedTo(category).total() %}
  
  &lt;p class="tags"&gt;
      {% set categories = craft.categories.group('markets') %}
      {% for category in categories %}
          {% set numofEntries = craft.entries.section('posts').limit(null).relatedTo({
              targetElement: category,
              field: marketName
          }).total() %}
          &lt;a href="{{ category.url }}"&gt;{{ category.title }}-{{numofEntries}}&lt;/a&gt;
      {% endfor %}
  &lt;/p&gt; 
  </code></pre>
  
  <p>Note: You don't actually need to feed the <code>targetElement</code> and <code>field</code> attributes to the <code>relatedTo()</code> parameter – simply <code>relatedTo(category)</code> will usually work just fine – but it's supposedly more performant to be as specific as possible about the relation you're looking for – in this case; any entries that have the <code>category</code> selected (i.e. the <code>targetElement</code>) in the Categories field <code>marketName</code>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-18T21:55:30.313Z
Answers.LastActivityDate: 2016-09-18T21:55:30.313Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>Okay, all I want to do is list all categories AND the # of entries associated with each category.</p>
  
  <pre><code>{% set numofEntries = craft.entries.section('posts').relatedTo(category).total() %}
  
  &lt;p class="tags"&gt;
      {% for categoryName in craft.categories.group('markets') %}
          &lt;a href="{{ categoryName.url }}"&gt;{{ categoryName.title }}-{{numofEntries}}&lt;/a&gt;
      {% endfor %}
  &lt;/p&gt; 
  </code></pre>
  
  <p>No error is thrown (and I am in dev mode), but the number that appears (49) is the same for all categories (and doesn't even equal the number of total entries).  </p>
  
  <p>A few things...  </p>
  
  <ul>
  <li><p>market group = markets ...</p></li>
  <li><p>the category field handler = marketName </p></li>
  </ul>
  
Tags: <templating>
Title: How do I print the number of entries assigned to a category?
ThreadText: # How do I print the number of entries assigned to a category?
  
  (Asked by: username_0 on 2016-09-18)
  
  Okay, all I want to do is list all categories AND the # of entries associated with each category.
  
  ```
  {% set numofEntries = craft.entries.section('posts').relatedTo(category).total() %}
  
  <p class="tags">
      {% for categoryName in craft.categories.group('markets') %}
          <a href="{{ categoryName.url }}">{{ categoryName.title }}-{{numofEntries}}</a>
      {% endfor %}
  </p> 
  
  ```
  
  No error is thrown (and I am in dev mode), but the number that appears (49) is the same for all categories (and doesn't even equal the number of total entries).
  
  A few things...
  
  * market group = markets ...
  * the category field handler = marketName
  
  **Tags:** templating
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2016-09-18. Score: 2 | Accepted answer
  
  You'll want to query for the number of entries *inside* the for loop.
  
  ```
  {% for categoryName in craft.categories.group('markets') %}
      {% set numofEntries = craft.entries.section('posts').relatedTo(categoryName).total() %}
  
      <a href="{{ categoryName.url }}">{{ categoryName.title }}-{{numofEntries}}</a>
  {% endfor %}
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2016-09-18. Score: 2
  
  If you want to display the total number of entries related to *each* category, you'll need to put the `{% set numofEntries ... %}` statement inside your `for` loop. You'll also need to change the `relatedTo(category)` part to `relatedTo(categoryName)` (or change the `categoryName` to `category`, which would probably be more appropriate).
  
  Something like this should work:
  
  ```
  {% set numofEntries = craft.entries.section('posts').relatedTo(category).total() %}
  
  <p class="tags">
      {% set categories = craft.categories.group('markets') %}
      {% for category in categories %}
          {% set numofEntries = craft.entries.section('posts').limit(null).relatedTo({
              targetElement: category,
              field: marketName
          }).total() %}
          <a href="{{ category.url }}">{{ category.title }}-{{numofEntries}}</a>
      {% endfor %}
  </p> 
  
  ```
  
  Note: You don't actually need to feed the `targetElement` and `field` attributes to the `relatedTo()` parameter – simply `relatedTo(category)` will usually work just fine – but it's supposedly more performant to be as specific as possible about the relation you're looking for – in this case; any entries that have the `category` selected (i.e. the `targetElement`) in the Categories field `marketName`.
  
  
  ---
  
  