8

I have a 'teacher' channel section on my site. That channel has three entry types, 'teacher', 'demoTeacher' and 'guestTeacher' - how do I display entries from teacher and demoTeach combined? At the moment I'm showing one type like this:

{% for teacher in craft.entries.find({section: 'teacher', type: 'demoTeacher'}) %}

Secondly, how can I use a conditional that checks for entry type? I need do something like the following (pseudocode):

{% if entrytype is demoTeacher %}
    display read more button
{% endif %}
11

You can set the ElementCriteriaModel's type parameter to multiple values by passing it an array of entry type slugs:

{% set teachers = craft.entries.section('teacher').type(['teacher', 'demoTeacher']) %}

Use the type property on the entry model to access its entry type in your conditional (also see this list for other available properties).

{% for teacher in teachers %}

    {{ teacher.title }}

    {% if teacher.type == 'demoTeacher' %}
        ...
    {% endif %}

{% endfor %}
  • Thanks - that works. Any idea how to use a conditional to test for entry type? – darylknight Sep 3 '14 at 22:06
  • Sure I have! :) – carlcs Sep 3 '14 at 22:08
3

I know an answer has been accepted for this question, but I wanted to provide another example for folks like me who seem to have a thick skull sometimes.

I have a "team" section which is a structure (the entries within this structure do not have their own urls). Within that structure, I have two different entry types. "Team Category" and "Team Member". This allows me to nest different team members under different categories.

For example,

Accounting
    - Bob Smith
Marketing
    - John Doe

Here is how I did that in my entry template:

{% set teamMembers = craft.entries.section('team').type(['teamCategory', 'teamMember']) %}
    {% for member in teamMembers %}
        <div class="team-member">
        {% if member.type == 'teamCategory' %}
            <h3>{{ member.title }}</h3> {# Accounting #}
        {% else %}
            <p>{{ member.title }}</p>  {# Bob Smith #}
            </div>
        {% endif %}
    {% endfor %}

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.