1

I want to display on entry view page, related entries to current tags. So far, I came to displaying basic entry's object, but I don't know How to get in matrix field and pull the first image.

Images are stored in matrixFieldHandle -> blockTypeHandle -> imageFieldHandle.

For outputting related entries to current tags I use:

{# Set ECM for related entries to current tags #}
{% set tag = entry.keywords %}
{% set relatedEntries = craft.entries.relatedTo(tag).id('not ' ~ entry.id).limit(8).find() %}


{% for entry in relatedEntries %}
    {{ entry.title }}
{% endfor %}
2

I think entry.matrixFieldHandle.type('blockTypeHandle') is what you're looking for. Putting it together, it'd be something like:

{% set tag = entry.keywords %}
{% set relatedEntries = craft.entries.relatedTo(tag).id('not ' ~ entry.id).limit(8) %}

{% for relatedEntry in relatedEntries %}

    {% set matrixBlock = relatedEntry.matrixFieldHandle.type('blockTypeHandle').first() %}

    {% if matrixBlock %}

        {% set asset = matrixBlock.imageFieldHandle.first() %}

        {% if asset %}

            <h1>{{ relatedEntry.title }}</h1>
            {{ asset.img }}

        {% endif %}

    {% endif %}

{% endfor %}
|improve this answer|||||
  • Somehow, I had it like that, before I've posted the question, except I moved the element criteria model outside the for loop to save DB query, I think. However I do, I get either the first image of all asset's images, or either none image. Sometimes I get message error - object couldn't be converted to string. – Dominik Krulak Apr 5 '15 at 9:19
  • At the first and this time, it's correct @Adam Gilleard. I was extending within if statement elseif, which was throwing me errors. Now it's fixed. Thanks – Dominik Krulak Apr 7 '15 at 11:01

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.