0

I am trying to loop through a matrix to output three blocks but also I want to pull in 3 images from 3 different sections. The images are asset fields in 3 other sections. should i loop through those sections using craft.entries or craft.assests? below is an example of what i have been trying out. thanks in advance for any help

{% for block in entry.homeMatrix %}
        {% if block.type == "homeMatrix" %}
                <a class="half-box full-box" href="{{ block.entry.first().url }}">
                    {% for image in craft.assets.kind('image') %}
                    <picture class="box-image">
                        <!--[if IE 9]><video style="display: none;"><![endif]-->
                            <source srcset="{{ entry.lTopBannerImg.first().url('gridImages480X340') }}" media="(min-width: 45em)">
                            <source srcset="{{ entry.lTopBannerImg.first().url('gridImageOdd559X315') }}" media="(min-width: 28.4375em)">
                            <source srcset="{{ entry.lTopBannerImg.first().url('gridImages560X520') }}">
                        <!--[if IE 9]></video><![endif]-->
                        <img alt="{{ entry.lTopBannerImgAltText.first().title }}" srcset="{{ entry.lTopBannerImg.first().url('gridImages480X340') }}">
                    </picture>
                    {% endfor %}
                    <div class="box-bar">
                        <h2>{{ block.url }}</h2>
                    </div>
                    <div class="see-more"><span class="plus">Click to see more information</span></div>

                </a>
        {% endif %}
    {% endfor %}
  • Are the three sections whose asset field you want to pull in Singles, or Channels? – Mats Mikkel Rummelhoff Jun 4 '15 at 9:01
  • They are singles :) – Steve Jun 4 '15 at 9:04
3

I'm not sure what {% for image in craft.assets.kind('image') %} is all about in your code since the image variable isn't being used anywhere.

But here's the basic structure on how you could loop through 3 separate Asset fields in 3 different single sections inside of a Matrix block:

{% for block in entry.homeMatrix %}
    {% if block.type == "homeMatrix" %}

        {% set firstSingle = craft.entries.section('myFirstSection').first() %}
        {% set secondSingle = craft.entries.section('mySecondSection').first() %}
        {% set thirdSingle = craft.entries.section('myThirdSection').first() %}

        {% if firstSingle.myAssetField|length %}
            {% for image in firstSingle.myAssetField %}
                {{ image.url }}
            {% endfor %}
        {% endif %}

        {% if secondSingle.myAssetField|length %}
            {% for image in secondSingle.myAssetField %}
                {{ image.url }}
            {% endfor %}
        {% endif %}

        {% if thirdSingle.myAssetField|length %}
            {% for image in thirdSingle.myAssetField %}
                {{ image.url }}
            {% endfor %}
        {% endif %}

    {% endif %}
{% endfor %}
| improve this answer | |

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.