<p>After using the above and re-reading the docs I got this to work.</p>

<pre><code>  {% set relatedGallery = entry.associatedGallery.first() %}
  {% if relatedGallery %}
    {% for matrixBlock in relatedGallery.albumImages %}
      {% set asset = matrixBlock.imageFile.first() %}
        {% if asset %}
          &lt;a href="{{ asset.getUrl() }}"&gt;&lt;img src="{{ asset.getUrl('homeThumb') }}" data-title="{{ matrixBlock.imageName }}"&gt;&lt;/a&gt;
        {% endif %}
    {% endfor %}    
  {% endif %}
</code></pre>

CC BY-SA 3.0
2016-08-21T03:01:35.667Z
2016-08-21T03:01:35.667Z
Phil
<p>Relationship field is defined like:</p>

<pre><code>{% for relatedItem in entry.relatedItemField %}
  {{ relatedItem.someField }} {{ relatedItem.someOtherField }}    
{% endfor %}
</code></pre>

<p>So maybe:</p>

<pre><code>{% if entry.associatedGallery | length %}
    {% for theGallery in entry.associatedGallery %}
        {% for matrixBlock in theGallery.albumImages %}
            {% if matrixBlock.imageFile | length %}
                {% for asset in matrixBlock.imageFile.first() %}
                    &lt;a href="{{ asset.getUrl() }}"&gt;&lt;img src="{{ asset.getUrl('homeThumb') }}" data-title="{{ matrixBlock.imageName }}"&gt;&lt;/a&gt;
                {% endfor %}
            {% endif %}
        {% endfor %}
    {% endfor %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2016-08-21T03:00:11.017Z
That seems to output a whole lot of info as the asset things like id, publish date and a whole bunch else that doesn't make sense to me but it did get me thinking and rereading the documentation on entry fields specifically. I think it was the .first() I was missing since I have it only set to be one related entry. I think your example might be missing a step where each related entry is defined. Not sure I will have to try a multiple relations example later.
Phil
CC BY-SA 3.0
2016-08-20T21:46:40.190Z
2016-08-20T21:46:40.190Z
AbbeyDesign
<p>I have a section that is a single type. On this page there is a relation ship ('entries') field where the user can select a related photo gallery. Photo gallery is its own section with a matrix field on it that contains image files and image names. How when I am displaying the single page pull in the matrix field to display an image gallery. This is what I have tried but not with much luck.</p>

<p>I have tried </p>

<pre><code>  {% if entry.associatedGallery is not null %}
    {% set theGallery = craft.entries.section('bb').relatedTo(entry.associatedGallery) %}
      &lt;div class="galleria associated-gallery"&gt;
        {% for matrixBlock in theGallery.albumImages %}
          {% set asset = matrixBlock.imageFile.first() %}
            {% if asset %}
              &lt;a href="{{ asset.getUrl() }}"&gt;&lt;img src="{{ asset.getUrl('homeThumb') }}" data-title="{{ matrixBlock.imageName }}"&gt;&lt;/a&gt;
            {% endif %}
        {% endfor %}
      &lt;/div&gt;
    {% else %}
      &lt;h1&gt; Apparently its null&lt;/h1&gt;  
  {% endif %}
</code></pre>

<p>and </p>

<pre><code>  {% if entry.associatedGallery is not null %}
    {% set theGallery = entry.associatedGallery %}
      &lt;div class="galleria associated-gallery"&gt;
        {% for matrixBlock in theGallery.albumImages %}
          {% set asset = matrixBlock.imageFile.first() %}
            {% if asset %}
              &lt;a href="{{ asset.getUrl() }}"&gt;&lt;img src="{{ asset.getUrl('homeThumb') }}" data-title="{{ matrixBlock.imageName }}"&gt;&lt;/a&gt;
            {% endif %}
        {% endfor %}
      &lt;/div&gt;
    {% else %}
      &lt;h1&gt; Apparently its null&lt;/h1&gt;  
  {% endif %}
</code></pre>

<p>I think I am missing something fundamental about how relationships are defined.</p>

CC BY-SA 3.0
2016-08-20T21:34:04.227Z
Are you pulling just the first image of the related gallery? Or do you want to show all the images of the related gallery?
AbbeyDesign
CC BY-SA 3.0
2016-08-21T02:38:39.860Z
Looking to pull all images in a gallery. The middle part of that code from 'for matrixBlock in.... is pretty much the same as I use on a gallery entry page. It just uses .first() because each block only contains one image and one title.
Phil
<matrix><relations>
Displaying images from a matrix field from a related field
# Displaying images from a matrix field from a related field

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

I have a section that is a single type. On this page there is a relation ship ('entries') field where the user can select a related photo gallery. Photo gallery is its own section with a matrix field on it that contains image files and image names. How when I am displaying the single page pull in the matrix field to display an image gallery. This is what I have tried but not with much luck.
I have tried 
  {% if entry.associatedGallery is not null %}
    {% set theGallery = craft.entries.section('bb').relatedTo(entry.associatedGallery) %}
      
        {% for matrixBlock in theGallery.albumImages %}
          {% set asset = matrixBlock.imageFile.first() %}
            {% if asset %}
              
            {% endif %}
        {% endfor %}
      
    {% else %}
       Apparently its null  
  {% endif %}

and 
  {% if entry.associatedGallery is not null %}
    {% set theGallery = entry.associatedGallery %}
      
        {% for matrixBlock in theGallery.albumImages %}
          {% set asset = matrixBlock.imageFile.first() %}
            {% if asset %}
              
            {% endif %}
        {% endfor %}
      
    {% else %}
       Apparently its null  
  {% endif %}

I think I am missing something fundamental about how relationships are defined.

**Tags:** matrix, relations
> **Comments:**
> **username_1**: Are you pulling just the first image of the related gallery? Or do you want to show all the images of the related gallery?  
> **username_0**: Looking to pull all images in a gallery. The middle part of that code from 'for matrixBlock in.... is pretty much the same as I use on a gallery entry page. It just uses .first() because each block only contains one image and one title.  

---

## 2 answers

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

After using the above and re-reading the docs I got this to work.
  {% set relatedGallery = entry.associatedGallery.first() %}
  {% if relatedGallery %}
    {% for matrixBlock in relatedGallery.albumImages %}
      {% set asset = matrixBlock.imageFile.first() %}
        {% if asset %}
          
        {% endif %}
    {% endfor %}    
  {% endif %}


---

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

Relationship field is defined like:
{% for relatedItem in entry.relatedItemField %}
  {{ relatedItem.someField }} {{ relatedItem.someOtherField }}    
{% endfor %}

So maybe:
{% if entry.associatedGallery | length %}
    {% for theGallery in entry.associatedGallery %}
        {% for matrixBlock in theGallery.albumImages %}
            {% if matrixBlock.imageFile | length %}
                {% for asset in matrixBlock.imageFile.first() %}
                    
                {% endfor %}
            {% endif %}
        {% endfor %}
    {% endfor %}
{% endif %}

> **Comments:**
> **username_0**: That seems to output a whole lot of info as the asset things like id, publish date and a whole bunch else that doesn't make sense to me but it did get me thinking and rereading the documentation on entry fields specifically. I think it was the .first() I was missing since I have it only set to be one related entry. I think your example might be missing a step where each related entry is defined. Not sure I will have to try a multiple relations example later.  

---

