0

I have a dropdown field with X options. I want to print each option as a button.

I tried...

<ul>
    {% for option in entry.isotope.options %}
        <li>{{ option }}</li>
    {% endfor %}
</ul>

isotope = dropdownFieldHandle

But I get the error Variable "entry" does not work'.

  • Are you actually trying to call this in an entry template? entry won't automatically get created by Craft if you're on a category or custom route, for example. – RitterKnight Feb 6 '17 at 23:30
  • No, I am not in the entry template. I tried using set to entry in craft.entries.section('posts'), but that just printed out the title of all the entries instead of the handful of options in the dropdown field. – Hellyar Feb 6 '17 at 23:38
  • 1
    That's your problem. Which post are you actually trying to get? When you call craft.entries you're going to get a group of entries back based on the parameters you specify. You might try something like {% set entry = craft.entries.section('posts').slug('post-with-isotope-options')[0] %} to get just one entry. – RitterKnight Feb 7 '17 at 0:00
  • But I don't want the entries. I just want to print the options in the field dropdown. – Hellyar Feb 7 '17 at 0:02
  • Gotcha, couple ways to do that, see answer below. – RitterKnight Feb 7 '17 at 0:35
4

There's not an "elegant" way to do this but you can access the dropdown field by its handle using getFieldByHandle. Something like:

{% set dropdownField = craft.fields.getFieldbyHandle('isotope') %}

  {% for option in dropdownField.settings.options %}
    <li>{{ option.value }}</li>
  {% endfor %}

If you need the label instead, just use {{ option.label }}

You can also grab a dropdown's options from any entry you know has the field already attached, such as the first entry from your news section for example:

{% set entry = craft.entries.section('news').limit(1)[0] %}

<ul>
{% for option in  entry.isotope.options %}
    <li>{{ option.value }}</li>
{% endfor %}
</ul>

Note: similar to this question.

| improve this answer | |
  • 1
    Your first approach is just what I needed to print the field options into the isotope data-filter. Thanks. – Hellyar Feb 7 '17 at 0:59

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.