1

I have set up 3 channel types - A, B & C and a Category. Each entry type shares the same pool of categories.

On the home page, I have created a Matrix field (homeSections) with a block type heroSections that has A, B & C in a dropdown (mediaType) and a Category select (homeSection), so I can have "select mediaType A that has Category 1" as an option.

This is what I have so far

{% set entry = craft.entries().section('home').one() %}

{% for block in entry.homeSections.all() %}

    {% switch block.type %}

    {% case "heroSections" %}

        {% for entry in block.homeSection.all() %}

            <h3>{{ entry.title }} {{ block.mediaType.label }}</h3>
            
            {% set section = block.mediaType %}
            {% set category = entry.slug %}
            {% set entries = craft.entries.section(section).limit(4).orderby('postDate desc').all() %}

            {% include '_includes/listing' %}
        {% endfor %}

{% endswitch %}

{% endfor %}

but I can't figure out how to then filter the results so that the category is applied to the entries returned.

1 Answer 1

1

I figured it out.

I changed

{% set category = entry.slug %}
{% set entries = craft.entries.section(section).limit(4).orderby('postDate desc').all() %}

to

{% set category = craft.categories.slug(entry.slug) %}
{% set entries = craft.entries.section(section).relatedTo(category).limit(4).orderby('postDate desc').all() %}

and it shows up as expected.

1
  • did rubber duck helped you? :) Commented Jul 4, 2020 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.