<p>I was able to figure this out on my own, based off this answer <a href="https://craftcms.stackexchange.com/questions/4536/get-related-entries-from-matrix-field">here.</a></p>

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

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

CC BY-SA 3.0
2016-08-09T21:53:11.170Z
2016-08-09T21:53:11.170Z
2017-04-13T12:51:00.200Z
Jesse Winton
<p>Something like this might work (obviously, replace the <code>matrixField</code>, <code>assetsField</code> and <code>imageTransform</code> handles with real values):</p>

<pre><code>{# 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 %}
            &lt;a href="{{ siblingEntry.url }}"&gt;
                &lt;img src="{{ galleryImage.getUrl('imageTransform') }}" /&gt;
            &lt;/a&gt;
        {% endif %}

    {% endif %}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2016-08-09T20:16:13.547Z
2016-08-09T20:21:30.883Z
2016-08-09T20:21:30.883Z
Mats Mikkel Rummelhoff
<p>I need to get Matrix field content from a entries sibling. Essentially, I have a block type called <code>gallery</code> 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 <code>gallery</code> block of the previous and next siblings and output it in this recent projects section. How can I do that?</p>

<p>Thanks,
J</p>

<templating><matrix><gallery>
Output Matrix Content from Sibling
# Output Matrix Content from Sibling

(Asked by: username_0 on 2016-08-09)

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

**Tags:** templating, matrix, gallery

---

## 2 answers

### Answer by username_0 on 2016-08-09. Score: 1

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 %}
  
    
      
      
        {{ entry.title }}
        View Project
       
      
  
  {% endfor %}
  {% endfor %}


---

### Answer by username_1 on 2016-08-09. Score: 1

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 %}
            
                
            
        {% endif %}

    {% endif %}

{% endfor %}


---

