1

I have a dropdown field that has three options. In my template, I want to do one thing if option 1 is selected and do something else if option 2 is selected.

Here is what my code looks like:

{% for option in entry.transparentMenu.options %}
    {% if entry.transparentMenu.value == "menuLight" %}
        <h3>white</h3>
    {% elseif entry.transparentMenu.value == "menuDark" %}
        <h3>black</h3>
    {% endif %}
{% endfor %}

The code seems to work but the output is: black black black; I only want black to display once.

What am I doing wrong?

1

You don't need the for loop in this case. What's happening is you're looping over each option in your dropdown field, and each time it's checking the value of the dropdown and displaying white or black. Since you have three options, you're going to get the dropdown value displayed three times (which is why you're seeing black black black).

All you need is just the if conditional:

{% if entry.transparentMenu.value == "menuLight" %}
    <h3>white</h3>
{% elseif entry.transparentMenu.value == "menuDark" %}
    <h3>black</h3>
{% endif %}

Also, fwiw looks like you have a typo in your code 👆️ - you use entry.transparentMenu twice and entry.header.transparentMenu once. I'm guessing you want entry.transparentMenu everywhere...

The only time you'd want to use the for loop to get all the dropdown field's options is if you wanted to display those in the template (in a list for example or to create your own dropdown on the frontend):

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

But in your case, you just want the current value and not all the options, so just remove the for loop.

|improve this answer|||||
  • Thanks man! That's it! I thought the for loop is important because in the matrix field you need it – adsfgfhg Dec 19 '18 at 14:08
  • For sure! So the difference is a matrix field has different blocks that you would loop over, but with a dropdown field you can quickly grab the value, which is all you need in this instance. There are cases where you would want to use a for loop to access the dropdown options in your template, but that's not super common. It all just depends on how you're using your dropdown field 👍 – Jalen Davenport Dec 19 '18 at 14:14
-1

I don't know your structure, but if transparentMenu is a select or checkbox or something with menuLight and menuDark as options, it should be:

{% for option in entry.transparentMenu.options %} {% if entry.header.transparentMenu.value == "menuLight" %} <h3>white</h3> {% elseif entry.header.transparentMenu.value == "menuDark" %} <h3>black</h3> {% endif %} {% endfor %}

instead of:

{% for option in entry.transparentMenu.options %} {% if entry.transparentMenu.value == "menuLight" %} <h3>white</h3> {% elseif entry.header.transparentMenu.value == "menuDark" %} <h3>black</h3> {% endif %} {% endfor %}

right?

|improve this answer|||||

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.