1

I have this in my edit form

{% set categories = craft.categories.group('jobCategory') %}
    <label for="fields[jobCategory]">Job Category:</label>
    <select id="jobCategory" name="fields[jobCategory]">
        {% for category in categories %}
            <option value="{{ category.id }}">{{ category.title }}</option>
        {% endfor %}
    </select>

but can't for the life of me, show select as 'selected' for the category the entry belongs to.

Missing something simple I'm sure.

1 Answer 1

2

Something like the following should work. This assumes that your category field handle is 'jobCategory', and has a limit set to 1.

{% set categories = craft.categories.group('jobCategory') %}
{% set entryCategory = entry.jobCategory.first %}
<label for="fields[jobCategory]">Job Category:</label>
<select id="jobCategory" name="fields[jobCategory]">
    {% for category in categories %}
        <option value="{{ category.id }}" {% if category.title == entryCategory %}selected{% endif %}>{{ category.title }}</option>
    {% endfor %}
</select>
3
  • BOOM! Almost {% if category.title == entryCategory %} - if you update your answer, I'll accept it :) Jan 20, 2015 at 17:46
  • @StevenGrant Updated for Douglas.
    – Brad Bell
    Jan 20, 2015 at 17:53
  • Close. Glad you got it working. (And thanks brad.) Jan 20, 2015 at 18:04

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.