<p>At the moment, you are loading <em>all</em> categories in the line <code>{% set categories = categoryEntry.all() %}</code> and that's why you are getting them all for each entries.</p>
<p>What you need to do is to fetch your categories in the context of your <code>blogEntry</code>.</p>
<p>Here I assume a field name of <code>blogCategories</code> on your <code>blogEntry</code> but change accordingly to match what your Categories field is call:</p>
<pre class="lang-twig prettyprint-override"><code>{% for blogEntry in craft.entries.section('blogPosts').limit(8).all() %}
  &lt;div class=&quot;blog card&quot;&gt;
    &lt;ul&gt;
      &lt;li&gt;Article&lt;/li&gt;
      &lt;li&gt;|&lt;/li&gt;
      {% nav category in blogEntry.blogCategories.all() %}
      &lt;li&gt;&lt;a href=&quot;{{ category.url }}&quot;&gt;&lt;/a&gt;{{ category.title }}&lt;/a&gt;&lt;/li&gt;
      {% endnav %}
    &lt;/ul&gt;
    &lt;h1&gt;{{ blogEntry.title }}&lt;/h1&gt;
    &lt;p&gt;{{ blogEntry.excerpt }}&lt;/p&gt;
  &lt;/div&gt;
{% endfor %}
</code></pre>
<p>Alternatively (and probably best) you can eager-load your categories on your initial entries query:</p>
<pre class="lang-twig prettyprint-override"><code>{# Fetch your entries and eager-load your categories #}
{% set blogEntries = craft.entries()
  .section('blogPosts')
  .with(['blogCategories'])
  .limit(8)
  .all() %}

{% for blogEntry in blogEntries %}
  &lt;div class=&quot;blog card&quot;&gt;
    &lt;ul&gt;
      &lt;li&gt;Article&lt;/li&gt;
      &lt;li&gt;|&lt;/li&gt;
      {% nav category in blogEntry.blogCategories %}
      &lt;li&gt;&lt;a href=&quot;{{ category.url }}&quot;&gt;&lt;/a&gt;{{ category.title }}&lt;/a&gt;&lt;/li&gt;
      {% endnav %}
    &lt;/ul&gt;
    &lt;h1&gt;{{ blogEntry.title }}&lt;/h1&gt;
    &lt;p&gt;{{ blogEntry.excerpt }}&lt;/p&gt;
  &lt;/div&gt;
{% endfor %}
</code></pre>
<p>You can read on eager-loading in the documentation here - <a href="https://craftcms.com/docs/3.x/dev/eager-loading-elements.html" rel="nofollow noreferrer">https://craftcms.com/docs/3.x/dev/eager-loading-elements.html</a></p>

CC BY-SA 4.0
2021-10-04T08:59:16.563Z
Thanks very much, eager-loading looks interesting
Grant Smith
CC BY-SA 4.0
2021-10-04T07:23:43.163Z
2021-10-04T07:23:43.163Z
Oli
<p>I have created a blog channel and a blog category along with a category field.</p>
<p>Now I wish output the category within the blog post.</p>
<pre class="lang-twig prettyprint-override"><code>{% set categoryEntry = craft.categories().group('blog') %}
{% set categories = categoryEntry.all() %}

  {% for blogEntry in craft.entries.section('blogPosts').limit(8).all() %}
  &lt;div class=&quot;blog card&quot;&gt;
    &lt;ul&gt;
      &lt;li&gt;Article&lt;/li&gt;
      &lt;li&gt;|&lt;/li&gt;
      {% nav category in categories %}
      &lt;li&gt;&lt;a href=&quot;{{ category.url }}&quot;&gt;&lt;/a&gt;{{ category.title }}&lt;/a&gt;&lt;/li&gt;
      {% endnav %}
    &lt;/ul&gt;
    &lt;h1&gt;{{ blogEntry.title }}&lt;/h1&gt;
    &lt;p&gt;{{ blogEntry.excerpt }}&lt;/p&gt;
  &lt;/div&gt;
  {% endfor %}
</code></pre>
<p>My code outputs all categories rather than just the one assiotated with the post.</p>
<p>Why is this?</p>

<craft3>
Output post category
# Output post category

(Asked by: username_0 on 2021-10-03)

I have created a blog channel and a blog category along with a category field.
Now I wish output the category within the blog post.
{% set categoryEntry = craft.categories().group('blog') %}
{% set categories = categoryEntry.all() %}

  {% for blogEntry in craft.entries.section('blogPosts').limit(8).all() %}
  
    
      Article
      |
      {% nav category in categories %}
      {{ category.title }}
      {% endnav %}
    
    {{ blogEntry.title }}
    {{ blogEntry.excerpt }}
  
  {% endfor %}

My code outputs all categories rather than just the one assiotated with the post.
Why is this?

**Tags:** craft3

---

## 1 answers

### Answer by username_1 on 2021-10-04. Score: 3 | Accepted answer

At the moment, you are loading all categories in the line {% set categories = categoryEntry.all() %} and that's why you are getting them all for each entries.
What you need to do is to fetch your categories in the context of your blogEntry.
Here I assume a field name of blogCategories on your blogEntry but change accordingly to match what your Categories field is call:
{% for blogEntry in craft.entries.section('blogPosts').limit(8).all() %}
  
    
      Article
      |
      {% nav category in blogEntry.blogCategories.all() %}
      {{ category.title }}
      {% endnav %}
    
    {{ blogEntry.title }}
    {{ blogEntry.excerpt }}
  
{% endfor %}

Alternatively (and probably best) you can eager-load your categories on your initial entries query:
{# Fetch your entries and eager-load your categories #}
{% set blogEntries = craft.entries()
  .section('blogPosts')
  .with(['blogCategories'])
  .limit(8)
  .all() %}

{% for blogEntry in blogEntries %}
  
    
      Article
      |
      {% nav category in blogEntry.blogCategories %}
      {{ category.title }}
      {% endnav %}
    
    {{ blogEntry.title }}
    {{ blogEntry.excerpt }}
  
{% endfor %}

You can read on eager-loading in the documentation here - https://craftcms.com/docs/3.x/dev/eager-loading-elements.html

> **Comments:**
> **username_0**: Thanks very much, eager-loading looks interesting  

---

