2

I feel like I'm missing something totally obvious here, but how do I output a category name for a category that is part of a category group on a single entry layout? The name of the category group is 'Archive' and the slug 'archive'.

Here is what I'm trying to do.

<a href="*dynamic url to category index*">*name of category*</a>

Here is what I tried

<a href="{{ category.url }}">{{ category.title }}</a>

However then I get the error "Variable "category" does not exist."

Trying the following code snippet from the official tutorial also did not work.

    {% for category in entry.postCategories.all() %}
      <a href="{{ category.url }}">
        {{- category.title -}}
      </a>
    {% endfor %}
  {% endif %}

resulting in the following error: "Calling unknown method: craft\elements\Entry::postCategories()"

Any help to solve this probably very simple problem is appreciated. Thank you!

  • You should replace postCategories with whatever the handle of the category field is for your entry. – Oli May 5 at 8:30
  • @Oli Brilliant, thank you so much! I'd been looking through the official tutorials, but could not find a straight forward answer that worked for me. It's much appreciated! – Maggie May 5 at 8:35
  • Good news! Could you mark this as answered please? – Oli May 5 at 8:42
2

You should replace postCategories with whatever the handle of the category field is for your entry.

| improve this answer | |
1

Thank you Oli for helping with this simple quest. Here's the code that worked for me. ("decade" being the name of the field I'm using in the backend, putting out the category group!)

    {% for category in entry.decade.all() %}
      <a href="{{ category.url }}"">
        {{- category.title -}}
      </a>
    {% endfor %}

  {% endif %}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.