2

I need to display different information depending on the section the entry comes under when I'm displaying all entries within a loop.

Use case - I have a feed which will pull in all entries such as 'Projects' and 'Events' both are sections set up as channels.

I need to display different information relating to each Entry on the front end as they have different fields.

So I need to check if the entry belongs to 'projects' or 'events' within the loop. I can't seem to achieve it.

Current code:

{% for entry in craft.entries.authorId(authorIds).orderBy('dateCreated desc') %}
                        
      {% if entry in craft.entries.section('projects') %}

      Do something
    
      {% elseif entry in craft.entries.section('events') %}

      Do something different

      {% endif %}

{% endfor %}

This displays nothing? Any help? Thanks

5

Check against entry.section.handle:

{% for entry in craft.entries.authorId(authorIds).orderBy('dateCreated desc').all() %}
                        
      {% if entry.section.handle == 'projects' %}

         Do something
    
      {% elseif entry.section.handle == 'events' %}

         Do something different

      {% endif %}

{% endfor %}
| improve this answer | |
  • Thanks James. That works! I had tried something similar to no avail.... {% if entry.section.name == 'projects' %} – Tom Byrom Jun 26 at 12:33
  • 2
    If you're ever in doubt about what variables are available to you, a handy trick is to dump out the key names in your template like this: <pre>{{ dump(entry.section|keys) }}</pre> – James Smith Jun 26 at 12:57
  • Good to know, appreciate that – Tom Byrom Jun 27 at 17: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.