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

<ul>
<li>Does the <code>Galleries</code> category have a url assigned? (make sure the "Categories in this group have their own URLs" box is checked)</li>
<li>Is there a category? You should <em>always</em> check, even if it is a required field. You can do <code>{% if block.linkTo.first() %}...{% endif %}</code> or <code>{{ block.linkTo.first().url() ?? '' }}</code> and use the <a href="https://github.com/TopShelfCraft/QQ" rel="nofollow noreferrer">A null coalescing operator</a>.</li>
</ul>

CC BY-SA 3.0
2016-11-11T08:30:22.373Z
That was it, needed the if statement
sarah3585
CC BY-SA 3.0
2016-11-10T17:30:39.773Z
2016-11-10T17:30:39.773Z
Marion Newlevant
<p>This is my Matrix set up:</p>

<p><a href="https://i.sstatic.net/AoxjL.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/AoxjL.png" alt="enter image description here"></a></p>

<p>I'm stumped to how to get the Categories URL.</p>

<pre><code>{% for block in entry.galleriesItem %}
    {% switch block.type %}

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

                    &lt;div class="bg-white panel__title"&gt;
                        &lt;span class="cta-link cta-link--black"&gt;{{ block.galleryType }}&lt;/span&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/a&gt;
    {% endswitch %}
{% endfor %}
</code></pre>

<p>I previously used:</p>

<pre><code>{{ block.linkTo.first().url() }}
</code></pre>

<p>when the field type was an entry.</p>

CC BY-SA 3.0
2016-11-10T16:27:18.247Z
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
CC BY-SA 3.0
2016-11-10T16:46:56.150Z
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
<matrix><categories><fieldtypes>
How to get Category URL from field in Matrix
# How to get Category URL from field in Matrix

(Asked by: username_0 on 2016-11-10)

This is my Matrix set up:

I'm stumped to how to get the Categories URL.
{% for block in entry.galleriesItem %}
    {% switch block.type %}

        {% case "galleriesItem" %}
            
                
                    {% for asset in block.image %}
                        
                    {% endfor %}

                    
                        {{ block.galleryType }}
                    
                
            
    {% endswitch %}
{% endfor %}

I previously used:
{{ block.linkTo.first().url() }}

when the field type was an entry.

**Tags:** matrix, categories, fieldtypes

> **Comments:**
> **username_1**: 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 }}  
> **username_0**: 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'  

---

## 1 answers

### Answer by username_2 on 2016-11-10. Score: 4 | Accepted answer

`{{ 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](https://github.com/TopShelfCraft/QQ).

> **Comments:**
> **username_0**: That was it, needed the if statement  

---

