I have a channel with entries which have a category field which is required. When the entries are listed on a page how do I find the category that it belongs to?

{% for entry in entries %}
    <span class="event">{{ entry.title }}</span>
{% endfor %}

Code so far is above.

The category has a background color field which I want to apply to the <span>.


If I'm following you, you've got a category group setup with a color field on its field layout.

Then you've got a category field setup with it's source set to that category group and in an entry, you want to get to the color field on the category group.

{% for entry in entries %}

    {# Grab the first category #}
    {% set category = entry.categoryFieldHandle.first() %}

        Or if there are multiple categories...
        set category = entry.categoryFieldHandle.find()
        And loop through to find the one you want

    <span class="event" style="background-color: {{ category.colorFieldHandle }}">{{ entry.title }}</span>
{% endfor %}

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.