This is my Matrix set up:

enter image description here

I'm stumped to how to get the Categories URL.

{% for block in entry.galleriesItem %}
    {% switch block.type %}

        {% case "galleriesItem" %}
            <a href="{{ block.linkTo.first().url() }}">
                <div class="grid-item panel {% if loop.index is divisible by(3) %}last{% endif %}">
                    {% for asset in block.image %}
                        <img src="{{ asset.getUrl }}"  alt="{{ asset.title }}"/>
                    {% endfor %}

                    <div class="bg-white panel__title">
                        <span class="cta-link cta-link--black">{{ block.galleryType }}</span>
    {% endswitch %}
{% endfor %}

I previously used:

{{ block.linkTo.first().url() }}

when the field type was an entry.

  • Can you copy and paste what it outputs for? <pre>{{ dump( block.linkTo.first() ) }}</pre> Also, what does it output when you do {{ block.linkTo.first().title }}
    – Brad
    Nov 10 '16 at 16:27
  • Output is massive, is there a certain part I could paste? With {{ block.linkTo.first().title }} I get the error 'Impossible to access an attribute ("title") on a null variable'
    – sarah3585
    Nov 10 '16 at 16:46

{{ block.linkTo.first().url() }} looks basically right, but a couple of things could be going wrong here:

  • Does the Galleries category have a url assigned? (make sure the "Categories in this group have their own URLs" box is checked)
  • Is there a category? You should always check, even if it is a required field. You can do {% if block.linkTo.first() %}...{% endif %} or {{ block.linkTo.first().url() ?? '' }} and use the A null coalescing operator.
  • That was it, needed the if statement
    – sarah3585
    Nov 11 '16 at 8:30

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.