1

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) %}
      <div class="galleria associated-gallery">
        {% for matrixBlock in theGallery.albumImages %}
          {% set asset = matrixBlock.imageFile.first() %}
            {% if asset %}
              <a href="{{ asset.getUrl() }}"><img src="{{ asset.getUrl('homeThumb') }}" data-title="{{ matrixBlock.imageName }}"></a>
            {% endif %}
        {% endfor %}
      </div>
    {% else %}
      <h1> Apparently its null</h1>  
  {% endif %}

and

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

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

  • 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 Aug 20 '16 at 21:34
  • 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 Aug 21 '16 at 2:38
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() %}
                    <a href="{{ asset.getUrl() }}"><img src="{{ asset.getUrl('homeThumb') }}" data-title="{{ matrixBlock.imageName }}"></a>
                {% endfor %}
            {% endif %}
        {% endfor %}
    {% endfor %}
{% endif %}
  • 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 Aug 21 '16 at 3:00
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 %}
          <a href="{{ asset.getUrl() }}"><img src="{{ asset.getUrl('homeThumb') }}" data-title="{{ matrixBlock.imageName }}"></a>
        {% endif %}
    {% endfor %}    
  {% endif %}

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.