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.

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>
  • BOOM! Almost {% if category.title == entryCategory %} - if you update your answer, I'll accept it :) – Steven Grant Jan 20 '15 at 17:46
  • @StevenGrant Updated for Douglas. – Brad Bell Jan 20 '15 at 17:53
  • Close. Glad you got it working. (And thanks brad.) – Douglas McDonald Jan 20 '15 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.