1

I have a hero textbox that I would like to control from the base template using a simple if/elseif.

I have a field called "hero" for the homepage. For pages in the "Small Groups" section, I want the hero to say "Groups." For everything else, I want it to be the entry title.

It seems straightforward, but this isn't working? It is ignoring the elseif.

{% if entry == craft.entries.section('homepage') %}
    {{ entry.hero }}
{% elseif entry == craft.entries.section('smallgroups') %}
    Groups
{% else %}
    {{ entry.title }}
{% endif %}
2

Your issue is probably that you're actually comparing an EntryModel (entry) to an ElementCriteriaModel (the array-like object used for querying entries, which is returned by the craft.entries call).

Something like this should do it:

{% if entry.section.handle == 'homepage' %}
    {{ entry.hero }}
{% elseif entry.section.handle == 'smallgroups' %}
    Groups
{% else %}
    {{ entry.title }}
{% endif %}

Assuming your section handles are correct, the above should work – although this is a perfect use for using the {% switch %} tag instead of {% if %}, which in my opinion would make your code more readable:

{% switch entry.section.handle %}
    {% case 'homepage' %}
        {{ entry.hero }}
    {% case 'smallgroups' %}
        Groups
    {% default %}
        {{ entry.title }}
{% endswitch %}
2
  • 1
    {% switch %} worked perfectly. I thought those only worked with matrix fields. Thanks for clarifying. – Jeff Fulton Sep 29 '17 at 22:30
  • @JeffFulton Nah, it's just a variation on the if/else statement. You can always get by with the latter, but code-wise it's often more concise when your conditional is based around multiple possible values for a single variable. To be clear, the use of if/else wasn't the reason your original code didn't work; it was the fact that you were comparing an EntryModel to an ElementCriteriaModel. – Mats Mikkel Rummelhoff Sep 29 '17 at 23:33

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.