<p>Set your section’s Entry URL Format to this:</p>

<pre><code>{mySection.first.slug}/{myCategory.first.slug}/{slug}
</code></pre>

<p><code>mySection.first</code> and <code>myCategory.first</code> will give you the first-selected category within those respective fields. Adding <code>.slug</code> after that will give you the slug of those categories.</p>

<p>If either of those category groups can have nested categories, you may want to change <code>.first</code> to <code>.last</code>, so the last-selected category’s slug makes it into the URL rather than the first one.</p>

<p><strong>Category URLs</strong></p>

<p>If you would like to make category URLs like this:</p>

<pre><code>mywebsite.nl/employees/health
</code></pre>

<p>you will need to create a new Route in Settings → Routes, with the following URL pattern:</p>

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

<p>and point the route to whatever template you want to load when these URLs are matched.</p>

<p>Then from your template, you can fetch the actual category models like this:</p>

<pre><code>{% set mySection = craft.categories({
    group: 'mySection',
    slug: matches[1]
}).first() %}

{% set myCategory = craft.categories({
    group: 'myCategory',
    slug: matches[2]
}).first() %}

{# Make sure the URL contained valid slugs #}
{% if not mySection or not myCategory) %}
    {% exit 404 %}
{% endif %}
</code></pre>

<p>From there you can do whatever you want with them. For example, you can display every entry that is related to both of them:</p>

<pre><code>{% set entries = craft.entries({
    relatedTo: ['and', {targetElement: mySection}, {targetElement: myCategory}]
}) %}

{% for entry in entries %}
    ...
{% endfor %}
</code></pre>

<p>You can repeat all of those steps to do a similar thing for URLs that just have one category in them as well (just omit the second route token, and ignore all the <code>myCategory</code> stuff in the example code).</p>

CC BY-SA 3.0
2016-04-07T14:40:41.530Z
Thanks Brandon,

But how do I set the URL format of myCategory when I want it to be a child of mySection?
Martijn La Feber
CC BY-SA 3.0
2016-04-07T18:32:27.527Z
@MartijnLaFeber I'm not sure I'm following. I made some edits to your question based on what I thought you meant. Can you review that and make sure I'm understanding your question correctly?
Brandon Kelly
CC BY-SA 3.0
2016-04-07T19:21:51.107Z
Your edits are ok, thanks for that. But I would like to know if it is possible to navigate via category urls also?
What do I have to fill in for the Category URL Formats?

`{mySection.first.slug}/{myCategory.first.slug}`
Martijn La Feber
CC BY-SA 3.0
2016-04-07T20:09:29.903Z
@MartijnLaFeber Just updated my answer with details on that.
Brandon Kelly
CC BY-SA 3.0
2016-04-06T19:17:02.003Z
2016-04-07T20:09:19.433Z
2016-04-07T20:09:19.433Z
Brandon Kelly
<p>I have a section with two Categories fields, and I would like the entries’ URLs to include the slugs of the categories selected in each of those fields.</p>

<p>The Category Groups are:</p>

<ul>
<li>mySection

<ul>
<li>Employees</li>
<li>Employers</li>
<li>Executives</li>
</ul></li>
<li>myCategory

<ul>
<li>Vitality</li>
<li>Health</li>
<li>Safety</li>
<li>Career</li>
</ul></li>
</ul>

<p>So an entry that has “Employees” selected in the first Categories field, ad “Health” selected in the second Categories field should end up with the URL:</p>

<pre><code>mywebsite.nl/employees/health/entrypage
</code></pre>

<p>How do I do this?</p>

<entries><categories><url>
Including two category slugs in entries’ URLs
# Including two category slugs in entries’ URLs

(Asked by: username_0 on 2016-04-06)

I have a section with two Categories fields, and I would like the entries’ URLs to include the slugs of the categories selected in each of those fields.
The Category Groups are:

mySection


Employees
Employers
Executives

myCategory


Vitality
Health
Safety
Career


So an entry that has “Employees” selected in the first Categories field, ad “Health” selected in the second Categories field should end up with the URL:
mywebsite.nl/employees/health/entrypage

How do I do this?

**Tags:** entries, categories, url

---

## 1 answers

### Answer by username_1 on 2016-04-06. Score: 4 | Accepted answer

Set your section’s Entry URL Format to this:
{mySection.first.slug}/{myCategory.first.slug}/{slug}

mySection.first and myCategory.first will give you the first-selected category within those respective fields. Adding .slug after that will give you the slug of those categories.
If either of those category groups can have nested categories, you may want to change .first to .last, so the last-selected category’s slug makes it into the URL rather than the first one.
Category URLs
If you would like to make category URLs like this:
mywebsite.nl/employees/health

you will need to create a new Route in Settings → Routes, with the following URL pattern:

and point the route to whatever template you want to load when these URLs are matched.
Then from your template, you can fetch the actual category models like this:
{% set mySection = craft.categories({
    group: 'mySection',
    slug: matches[1]
}).first() %}

{% set myCategory = craft.categories({
    group: 'myCategory',
    slug: matches[2]
}).first() %}

{# Make sure the URL contained valid slugs #}
{% if not mySection or not myCategory) %}
    {% exit 404 %}
{% endif %}

From there you can do whatever you want with them. For example, you can display every entry that is related to both of them:
{% set entries = craft.entries({
    relatedTo: ['and', {targetElement: mySection}, {targetElement: myCategory}]
}) %}

{% for entry in entries %}
    ...
{% endfor %}

You can repeat all of those steps to do a similar thing for URLs that just have one category in them as well (just omit the second route token, and ignore all the myCategory stuff in the example code).

> **Comments:**
> **username_0**: Thanks Brandon,  But how do I set the URL format of myCategory when I want it to be a child of mySection?  
> **username_1**: @MartijnLaFeber I'm not sure I'm following. I made some edits to your question based on what I thought you meant. Can you review that and make sure I'm understanding your question correctly?  
> **username_0**: Your edits are ok, thanks for that. But I would like to know if it is possible to navigate via category urls also? What do I have to fill in for the Category URL Formats?  `{mySection.first.slug}/{myCategory.first.slug}`  
> **username_1**: @MartijnLaFeber Just updated my answer with details on that.  

---

