Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-04T08:59:16.563Z
Answers.Text: Thanks very much, eager-loading looks interesting
Answers.UserDisplayName: Grant Smith
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-04T07:23:43.163Z
Answers.LastActivityDate: 2021-10-04T07:23:43.163Z
Answers.UserDisplayName: Oli
Body: <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>
  
Tags: <craft3>
Title: Output post category
ThreadText: # 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.
  
  ```twig
  {% set categoryEntry = craft.categories().group('blog') %}
  {% set categories = categoryEntry.all() %}
  
    {% for blogEntry in craft.entries.section('blogPosts').limit(8).all() %}
    <div class="blog card">
      <ul>
        <li>Article</li>
        <li>|</li>
        {% nav category in categories %}
        <li><a href="{{ category.url }}"></a>{{ category.title }}</a></li>
        {% endnav %}
      </ul>
      <h1>{{ blogEntry.title }}</h1>
      <p>{{ blogEntry.excerpt }}</p>
    </div>
    {% 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:
  
  ```twig
  {% for blogEntry in craft.entries.section('blogPosts').limit(8).all() %}
    <div class="blog card">
      <ul>
        <li>Article</li>
        <li>|</li>
        {% nav category in blogEntry.blogCategories.all() %}
        <li><a href="{{ category.url }}"></a>{{ category.title }}</a></li>
        {% endnav %}
      </ul>
      <h1>{{ blogEntry.title }}</h1>
      <p>{{ blogEntry.excerpt }}</p>
    </div>
  {% endfor %}
  
  ```
  
  Alternatively (and probably best) you can eager-load your categories on your initial entries query:
  
  ```twig
  {# Fetch your entries and eager-load your categories #}
  {% set blogEntries = craft.entries()
    .section('blogPosts')
    .with(['blogCategories'])
    .limit(8)
    .all() %}
  
  {% for blogEntry in blogEntries %}
    <div class="blog card">
      <ul>
        <li>Article</li>
        <li>|</li>
        {% nav category in blogEntry.blogCategories %}
        <li><a href="{{ category.url }}"></a>{{ category.title }}</a></li>
        {% endnav %}
      </ul>
      <h1>{{ blogEntry.title }}</h1>
      <p>{{ blogEntry.excerpt }}</p>
    </div>
  {% 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  
  
  ---
  
  