1

I'm currently trying to create a for-loop that loops through all options of a dropdown field created in Craft.

I have a field called "Season", with the possible options to choose from being "2015/16" and "2016/17". What I'm trying to do now is to create a loop that would loop as many times as there are options available in that field.

I'm thinking of something like this:

{% for option in fields.season %}
    ...code...
{% endfor %}`

Is there a way to accomplish this within Craft?

2
{% for option in entry.dropdownFieldHandle.options %}
    <li>{{ option }}</li>
{% endfor %}

That should be it :)

{% for entry in craft.entries.section('sectionname') %}
  {% for option in entry.dropdownFieldHandle.options %}
    <li>{{ option }}</li>
  {% endfor %}
{% endfor %}

Use the above if it is only one page

{% set myArray = [] %}

{% for entry in craft.entries.section('sectionname') %}
  {% for option in entry.dropdownFieldHandle.options %}
        {% set value = option %}

        {% set myArray = myArray|merge([value]) %}
  {% endfor %}
{% endfor %}

And loop over that variable , better to load 1 variable as array if you need it over multiple pages. Just make sure all pages have access to this variable (or create a macro for it)

  • also if you want the fields in general use the following: {% set dropdown = craft.fields.getFieldByHandle('season') %} {% for option in dropdown.options %} {{ option }} {% endif %} – mike-orez Nov 7 '16 at 14:33
  • I'd guess you meant the last {% endif %} to be a {% endfor %}, but even then craft gives me the error that there is no scope called "options". – justussimon91 Nov 7 '16 at 15:01
  • I edited my answers, one of the solutions should work. – mike-orez Nov 7 '16 at 15:36
  • Your first code snipped does not produce any output at all for me. The second does, but outputs every season as often as there are entries in the section I chose for 'sectionname' instead of just once as required. Still appreciated :) – justussimon91 Nov 8 '16 at 7:37
  • I got it solved by just limiting the for entry in craft.entries.section('sectionname') to 1 by appending .limit(1) – justussimon91 Nov 8 '16 at 7:48

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.