Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-12T10:08:04.713Z
Answers.LastActivityDate: 2021-05-12T10:08:04.713Z
Answers.UserDisplayName: MoritzLost
Body: <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>
  
Tags: <templating><categories><field>
Title: Use same handle for different fields on different entry types
ThreadText: # 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:
  
  ```
  <h2>{{ entry.title }}</h2>
  <p>{{ entry.category.all()|column('title')|join(', ') }}</p>
  
  ```
  
  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](https://craftcms.com/docs/3.x/extend/extending-system-components.html#behaviors) 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) %}
      <p>{{ categoryQuery.all()|column('title')|join(', ') }}</p>
  {% endmacro %}
  
  {# blog/_entry.twig #}
  {% from "macros/macros.twig" import category_list %}
  
  <h2>{{ entry.title }}</h2>
  <p>{{ category_list(entry.blogCategories) }}</p>
  
  {# job/_entry.twig #}
  {% from "macros/macros.twig" import category_list %}
  
  <h2>{{ entry.title }}</h2>
  <p>{{ category_list(entry.jobCategories) }}</p>
  
  ```
  
  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 %}
      <p>{{ categories|column('title')|join(', ') }}</p>
  {% endmacro %}
  
  {# entry/_entry.twig #}
  {% from "macros/macros.twig" import category_list %}
  
  <h2>{{ entry.title }}</h2>
  <p>{{ category_list(entry) }}</p>
  
  ```
  
  
  ---
  
  