<p>I might be mistaken, but you <em>can</em> just insert an <code>if</code> block into your loop:</p>

<pre><code>{% set excludedId = 1234 %}

{% for category in entry.categories.all() %}
  {% if category.id != excludedId %}
    &lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;
    {{ not loop.last ? '|' }}
  {% endif %}
{% endfor %}
</code></pre>

<p>That said, this will no longer know whether the "last" item in the loop was excluded. A better solution might be to trim down the collection <em>before</em> beginning the iteration:</p>

<pre><code>{% set excludedId = 1234 %}

{% for category in entry.categories.all() | filter((c) =&gt; c.id != excludedId) %}
  &lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;
  {{ not loop.last ? '|' }}
{% endfor %}
</code></pre>

<p>This makes use of the <a href="https://twig.symfony.com/doc/3.x/filters/filter.html" rel="nofollow noreferrer"><code>filter</code> filter</a>, which you can pass a "function" of sorts to compare each ID to your excluded one. If the comparison is <code>false</code>, it'll remove it from the collection before the loop starts.</p>

<p>One last option (and possibly the best one) is to adjust the query itself, before it's run:</p>

<pre><code>{% set excludedId = 1234 %}
{% set onlyDesiredCategories = entry.categories.id("not #{excludedId}").all() %}

{% for category in onlyDesiredCategories %}
  &lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;
  {{ not loop.last ? '|' }}
{% endfor %}
</code></pre>

<p>Instead of filtering down from the natural relations, this method attempts to adjust the <a href="https://github.com/craftcms/cms/blob/69faaee88ab6b797fe8fb95e2c525271f4f094ef/src/fields/BaseRelationField.php#L354-L411" rel="nofollow noreferrer"><code>ElementQuery</code> instance provided by the relation field</a> (<code>entry.categories</code>) by setting the <code>id</code> param to <code>not 1234</code>, meaning that ID will be excluded when the query is executed. <em>I haven't actually tried this lat method, myself, so your mileage may vary.</em></p>

CC BY-SA 4.0
2019-12-27T03:42:45.240Z
#3 worked for me! Thanks very much.
Tyssen
CC BY-SA 4.0
2019-12-24T19:20:55.797Z
2019-12-24T19:20:55.797Z
August Miller
<p>With </p>

<pre><code>{% for category in entry.categories.all() %}
    &lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt; {{ not loop.last ? '| ' }}
{% endfor %}
</code></pre>

<p>How do I exclude a specific category from displaying?</p>

<entries><categories>
Don't output specific category on entry page
# Don't output specific category on entry page

(Asked by: username_0 on 2019-12-24)

With 
{% for category in entry.categories.all() %}
    {{ category.title }} {{ not loop.last ? '| ' }}
{% endfor %}

How do I exclude a specific category from displaying?

**Tags:** entries, categories


---

## 1 answers

### Answer by username_1 on 2019-12-24. Score: 2 | Accepted answer

I might be mistaken, but you can just insert an if block into your loop:
{% set excludedId = 1234 %}

{% for category in entry.categories.all() %}
  {% if category.id != excludedId %}
    {{ category.title }}
    {{ not loop.last ? '|' }}
  {% endif %}
{% endfor %}

That said, this will no longer know whether the "last" item in the loop was excluded. A better solution might be to trim down the collection before beginning the iteration:
{% set excludedId = 1234 %}

{% for category in entry.categories.all() | filter((c) => c.id != excludedId) %}
  {{ category.title }}
  {{ not loop.last ? '|' }}
{% endfor %}

This makes use of the filter filter, which you can pass a "function" of sorts to compare each ID to your excluded one. If the comparison is false, it'll remove it from the collection before the loop starts.
One last option (and possibly the best one) is to adjust the query itself, before it's run:
{% set excludedId = 1234 %}
{% set onlyDesiredCategories = entry.categories.id("not #{excludedId}").all() %}

{% for category in onlyDesiredCategories %}
  {{ category.title }}
  {{ not loop.last ? '|' }}
{% endfor %}

Instead of filtering down from the natural relations, this method attempts to adjust the ElementQuery instance provided by the relation field (entry.categories) by setting the id param to not 1234, meaning that ID will be excluded when the query is executed. I haven't actually tried this lat method, myself, so your mileage may vary.

> **Comments:**
> **username_0**: #3 worked for me! Thanks very much.  

---

