Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-11T08:30:22.373Z
Answers.Text: That was it, needed the if statement
Answers.UserDisplayName: sarah3585
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-10T17:30:39.773Z
Answers.LastActivityDate: 2016-11-10T17:30:39.773Z
Answers.UserDisplayName: Marion Newlevant
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-11-10T16:27:18.247Z
Comments.Text: 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 }}
Comments.UserDisplayName: Brad
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-11-10T16:46:56.150Z
Comments.Text: 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'
Comments.UserDisplayName: sarah3585
Tags: <matrix><categories><fieldtypes>
Title: How to get Category URL from field in Matrix
ThreadText: # How to get Category URL from field in Matrix
  
  (Asked by: username_0 on 2016-11-10)
  
  This is my Matrix set up:
  
  [![enter image description here](https://i.sstatic.net/AoxjL.png)](https://i.sstatic.net/AoxjL.png)
  
  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>
                      </div>
                  </div>
              </a>
      {% 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  
  
  ---
  
  