I need to get Matrix field content from a entries sibling. Essentially, I have a block type called gallery within my project pages, and under the project content on each page, I have a section for Other Recent Projects. I'd like to be able to get the first image from the gallery block of the previous and next siblings and output it in this recent projects section. How can I do that?

Thanks, J

2 Answers 2


Something like this might work (obviously, replace the matrixField, assetsField and imageTransform handles with real values):

{# Basic query parameters for your sibling entries #}
{% set siblingCriteria = craft.entries.section(entry.section.handle) %}

{# Creates an array with the next and previous entries, based on the basic query parameters #}
{% set siblingEntries = [entry.getPrev(siblingCriteria), entry.getNext(siblingCriteria)] %}

{# Loop through the next and previous entries
{% for siblingEntry in siblingEntries %}

    {% if siblingEntry %}{# This might be null, if there isn't a next or previous entry #}

        {# Get the first `gallery` block for the entry's Matrix field #}
        {% set galleryBlock = siblingEntry.matrixField.type('gallery').first() %}

        {# Get the first asset in the gallery block's Assets field #}
        {% set galleryImage = galleryBlock ? galleryBlock.assetsField.first() : null %}

        {# Render the image #}
        {% if galleryImage %}
            <a href="{{ siblingEntry.url }}">
                <img src="{{ galleryImage.getUrl('imageTransform') }}" />
        {% endif %}

    {% endif %}

{% endfor %}

I was able to figure this out on my own, based off this answer here.

Here is the code I ended up using to achieve what I needed, used inside my matrix field.

{% if block.type == "featuredProjects" %}
  {% set relatedEntries = craft.entries.section('projects').relatedTo(block).limit(12) %}
  {% for entry in relatedEntries %}
  {% for asset in entry.mainHero %}
  <figure id="{{ entry.slug }}" class="featured-item position-relative">
    <a href="{{ entry.url }}">
      <img src="{{ asset.url }}">
      <figcaption class="project-info position-absolute gap-left-right pin-top-center">
        <h3 class="title">{{ entry.title }}</h3>
        <span>View Project</span>
  {% endfor %}
  {% endfor %}

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.