<p>You need two different fields since you can only select one category group per category field. As you said, having two fields with the same name isn't possible. One way to work around this would be to add a <a href="https://craftcms.com/docs/3.x/extend/extending-system-components.html#behaviors" rel="nofollow noreferrer">behaviour</a> that checks the entry type and returns the categories from the corresponding field (i.e. <code>jobCategories</code> for <code>job</code> entries and <code>blogCategories</code> for <code>blog</code> entries).</p>
<p>However, this is more complicated than it needs to be. You can just define a macro and pass it the appropriate field depending on the template:</p>
<pre><code>{# macros/macros.twig #}
{% macro category_list(categoryQuery) %}
    &lt;p&gt;{{ categoryQuery.all()|column('title')|join(', ') }}&lt;/p&gt;
{% endmacro %}


{# blog/_entry.twig #}
{% from &quot;macros/macros.twig&quot; import category_list %}

&lt;h2&gt;{{ entry.title }}&lt;/h2&gt;
&lt;p&gt;{{ category_list(entry.blogCategories) }}&lt;/p&gt;


{# job/_entry.twig #}
{% from &quot;macros/macros.twig&quot; import category_list %}

&lt;h2&gt;{{ entry.title }}&lt;/h2&gt;
&lt;p&gt;{{ category_list(entry.jobCategories) }}&lt;/p&gt;
</code></pre>
<p>If you have a single entry template for both entry types, you can also check the different fields inside the macro:</p>
<pre><code>{# macros/macros.twig #}
{% macro category_list(entry) %}
    {% switch entry.type.handle %}
        {% case &quot;job&quot; %}
            {% set categories = entry.jobCategories.all() %}
        {% case &quot;blog&quot; %}
            {% set categories = entry.blogCategories.all() %}
        {% default %}
            {% set categories = [] %}
    {% endswitch %}
    &lt;p&gt;{{ categories|column('title')|join(', ') }}&lt;/p&gt;
{% endmacro %}


{# entry/_entry.twig #}
{% from &quot;macros/macros.twig&quot; import category_list %}

&lt;h2&gt;{{ entry.title }}&lt;/h2&gt;
&lt;p&gt;{{ category_list(entry) }}&lt;/p&gt;
</code></pre>

CC BY-SA 4.0
2021-05-12T10:08:04.713Z
2021-05-12T10:08:04.713Z
MoritzLost
<p>I've got two entry types: <code>blog</code> and <code>job</code>. Each has a category field but different category groups: <code>blogCategories</code> and <code>jobCategories</code>. I would like to write a Twig include to accept either a blog or a job and display its categories like:</p>
<pre><code>&lt;h2&gt;{{ entry.title }}&lt;/h2&gt;
&lt;p&gt;{{ entry.category.all()|column('title')|join(', ') }}&lt;/p&gt;
</code></pre>
<p>Is there any way to do this? I tried creating two fields, both called <code>categories</code> but the field handle has to be unique. Is there such a thing as a field alias?</p>

<templating><categories><field>
Use same handle for different fields on different entry types
# Use same handle for different fields on different entry types

(Asked by: username_0 on 2021-05-11)

I've got two entry types: blog and job. Each has a category field but different category groups: blogCategories and jobCategories. I would like to write a Twig include to accept either a blog or a job and display its categories like:
{{ entry.title }}
{{ entry.category.all()|column('title')|join(', ') }}

Is there any way to do this? I tried creating two fields, both called categories but the field handle has to be unique. Is there such a thing as a field alias?

**Tags:** templating, categories, field


---

## 1 answers

### Answer by username_1 on 2021-05-12. Score: 2 | Accepted answer

You need two different fields since you can only select one category group per category field. As you said, having two fields with the same name isn't possible. One way to work around this would be to add a behaviour that checks the entry type and returns the categories from the corresponding field (i.e. jobCategories for job entries and blogCategories for blog entries).
However, this is more complicated than it needs to be. You can just define a macro and pass it the appropriate field depending on the template:
{# macros/macros.twig #}
{% macro category_list(categoryQuery) %}
    {{ categoryQuery.all()|column('title')|join(', ') }}
{% endmacro %}


{# blog/_entry.twig #}
{% from "macros/macros.twig" import category_list %}

{{ entry.title }}
{{ category_list(entry.blogCategories) }}


{# job/_entry.twig #}
{% from "macros/macros.twig" import category_list %}

{{ entry.title }}
{{ category_list(entry.jobCategories) }}

If you have a single entry template for both entry types, you can also check the different fields inside the macro:
{# macros/macros.twig #}
{% macro category_list(entry) %}
    {% switch entry.type.handle %}
        {% case "job" %}
            {% set categories = entry.jobCategories.all() %}
        {% case "blog" %}
            {% set categories = entry.blogCategories.all() %}
        {% default %}
            {% set categories = [] %}
    {% endswitch %}
    {{ categories|column('title')|join(', ') }}
{% endmacro %}


{# entry/_entry.twig #}
{% from "macros/macros.twig" import category_list %}

{{ entry.title }}
{{ category_list(entry) }}


---

