1

All, I'm very close, but missing something and any help is appreciated. I have a teamMember matrix block allowing me to add employees with some basic info. One of the fields is a category field called 'department'. I"m trying to list the departments, and within them, the employee information.

Currently my output is this:

Marketing
  marketing member one
  marketing member two
  creative member one
  creative member two

Creative
  marketing member one
  marketing member two
  creative member one
  creative member two

However, I'm looking to achieve this:

Marketing 
  marketing member one
  marketing member two

Creative
 creative member one
 creative member two

It seems that I'm doing something wrong with relatedTo.

My code:

        {# Fetch entries with a Matrix block to get non empty categories #}
        {% for block in entry.teamMember.all() %}
        {% set category = block.department.one() %}

        {# Fetch entries with a Matrix block related to the category #}
        {% set entries = craft.entries.relatedTo(category).all() %}

        <section id="{{ category.title }}">
            <h1>{{ category.title }}</h1>
            {% for block in entry.teamMember.all() %}
            <section class="member">
                {% set image = block.photograph.one() %}
                {% if image %}
                <div style="background-image: url('{{ image.getUrl('image500') }}');">&nbsp;</div>
                {% endif %}

                <h1>{{ block.fullName }} <em>{{ block.jobTitle|markdown }}</em></h1>
                {{ block.biography }}
            </section>
            {% endfor %}

        </section>
        {% endfor %}

What am I missing? Thank you in advance!

1

Figured it out:

    {# Fetch entries with a Matrix block to get non empty categories #}
    {% for block in entry.teamMember.all() %}
    {% set category = block.department.one() %}

    <section id="{{ category.title }}">
        {% if category.title != 'Ownership' %}<h1>{{ category.title }}</h1>{% endif %}
        {% for block in entry.teamMember.relatedTo(category).all() %}
        <section class="member">
            {% set image = block.photograph.one() %}
            {% if image %}
            <div style="background-image: url('{{ image.getUrl('image500') }}');">&nbsp;</div>
            {% endif %}

            <h1>{{ block.fullName }} <em>{{ block.jobTitle|markdown }}</em></h1>
            {{ block.biography }}
        </section>
        {% endfor %}

    </section>
    {% 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.