1

So I am using the matrix field to enable uploading of images onto a slideshow for the homepage section. My goal is to have one of the images link to another URL when you click on it. Basic stuff when using plain HTML, but I have it setup like this in Twig:

{% for block in entry.information %}
{% if block.type == "images" %}
   {% set image = block.image.first() %}
       {% if image %}
            <a href={{ block.anchor | cacheBuster }} target="_blank">
                <img class="slideshow" src={{ image.url | cacheBuster }} alt={{ image.title }}>
            </a>
        {% endif %}
    {% endif %}
{% endfor %}

This code runs fine and my slideshow images are all displayed using just one loop.

The problem is, all my slides are having an anchor field activated. I only want one of the slides to have a link to a URL, however, in the future I may want to add links to other slides so I need something that is also dynamic enough to support this.

My whole entire approach may be wrong and I might need to use a completely different approach... assistance is greatly appreciated.

3

Try something like:

{% for block in entry.information %}
{% if block.type == "images" %}
    {% set image = block.image.first() %}
        {% if image %}
            {% if block.anchor |length %}
            <a href={{ block.anchor | cacheBuster }} target="_blank">
                <img class="slideshow" src={{ image.url | cacheBuster }} alt={{ image.title }}>
            </a>
            {% else %}
            <img class="slideshow" src={{ image.url | cacheBuster }} alt={{ image.title }}>
            {% endif %}
        {% endif %}
    {% endif %}
{% endfor %}

So that only if the block.anchor field has something in it do you generate the link & the image. Otherwise, you just generate the image.

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.