<p>If you go into your Category Group’s settings and set the following settings:</p>

<ul>
<li>Craft 2

<ul>
<li>Top Level URL Format: <code>{slug}</code></li>
<li>Nested URL Format: <code>{parent.uri}/{slug}</code></li>
</ul></li>
<li>Craft 3

<ul>
<li>Category URI Format: <code>{parent.uri}/{slug}</code></li>
</ul></li>
</ul>

<p>then you would be able to set your section’s Entry URL Format to this:</p>

<ul>
<li><p>Craft 2</p>

<pre><code>{myCategoryField.last.uri}/{slug}
</code></pre></li>
<li><p>Craft 3</p>

<pre><code>{myCategoryField.inReverse().one().uri}/{slug}
</code></pre></li>
</ul>

CC BY-SA 3.0
2014-07-10T09:33:32.290Z
This is handy. Is there an easy way to just output just the child category and not include the parent?
shorn
CC BY-SA 3.0
2014-07-10T13:14:58.320Z
@shorn Not sure I’m following. Maybe post as its own question with more details?
Brandon Kelly
CC BY-SA 3.0
2014-07-10T17:05:10.327Z
Ok, will post up a question shortly.
shorn
CC BY-SA 4.0
2019-06-18T16:11:57.127Z
In regards to the Category URI Format, if you have a prefix before all category URI's, the example given here for Craft 3 results in the prefix being duplicated. In this case the following solution worked for me: `prefix/{parent.slug}/{slug}` (using `parent.slug` rather than `parent.uri`).
Mike
CC BY-SA 4.0
2020-08-16T18:42:41.137Z
@Mike - The problem with your solution prefix/{parent.slug}/{slug} is, that its only for 2 levels. The gandparent and the grand-grand-grandparent are not sluggified with this method. 
Do u know what to do in this case?
Slowwie
CC BY-SA 4.0
2020-08-17T05:05:41.803Z
I dont really understand what this section´s Entry URL Format is. 
After I changed the Category URI Format of the Category Group - I can get now 2 level URI's. Fine but what if my customer is nesting more levels. 
What do u mean with section's Entry URL format?
As I can see there is no section for Categories...
Slowwie
CC BY-SA 4.0
2020-08-17T12:19:55.627Z
@Slowwie This question is asking how to set _entries’_ URLs based on their selected categories. If you are just concerned with setting category URLs, you can safely ignore the part about sections (which contain entries). The category URL format above will work no matter how many levels you have.
Brandon Kelly
CC BY-SA 3.0
2014-06-21T05:10:43.110Z
2018-02-27T14:46:47.437Z
2018-02-27T14:46:47.437Z
Brandon Kelly
<p>I'm trying to mimic the URL structure exactly how I have it in my old CMS. If I select an entry to have both a category and subcategory I would like to have the subcategory in the URL along with the category and the entry slug.</p>

<p>For Example</p>

<pre><code>http://example.com/cat1/subcat1/entry
</code></pre>

<p>Right now I have the Entry URL Format set to be <code>{category.first().slug}/{slug}</code>, but that only shows the first category. How do I access that optional <code>subcat1</code> subcategory in that field?</p>

<p>EDIT</p>

<p>I have tried the following but it doesn't work</p>

<pre><code>{category.first.slug }/{ category.first.children.first.slug ? category.first.children.first.slug ~ '/' : '' }{slug}
</code></pre>

<p>I have also tried </p>

<pre><code>{category[0].slug}/{ category[1] ? category[1].slug ~ '/': ''}{slug}
</code></pre>

CC BY-SA 3.0
2014-06-20T18:46:13.360Z
Can you try and use double curly braces {{ .. }} , if I remember correctly there was a change in Craft 2.0 which makes them necessary.
Victor
CC BY-SA 3.0
2014-06-20T18:48:18.783Z
Just tried that, no difference unfortunately.
Seán O'Grady
CC BY-SA 3.0
2014-06-20T20:01:07.677Z
It seems that you want both of the following to load the entry template:

http://example.com/cat1/subcat1/entry & 
http://example.com/cat1/entry

Is this correct?

Probably you will need to use custom routes, and a bit of template logic to determine if a request segment is a category url or an entry url.  

I've not run into a need for segment 1 as a category url, it has always been something like /catalog/cat/entry or news/cat/entry/  Is there a specific reason you are not doing this way?
Steve Adams
CC BY-SA 3.0
2014-06-20T20:03:59.997Z
Because the CMS I used before – Wordpress – structures posts with subcategories in the URL in this way. To be honest it makes the most sense, otherwise what is the point of subcategories in the first place?
Seán O'Grady
CC BY-SA 3.0
2014-06-20T20:11:43.977Z
So .com/cat1/ would list all entries in cat1 including its subs, and .com/cat1/sub1/ would list only sub1 entries? Then you have .com/cat1/entryA/ and .com/cat1/sub1/entryA/ ?  Craft is so flexible, you can make it behave however you need.  It will help if you update your question with more example urls and what you expect to happen with each one. :)
Steve Adams
CC BY-SA 3.0
2014-06-20T21:03:23.213Z
It is a bit strange I agree, but the category urls I have set to `example.com/category/<category-name>/` The category slugs in the entry URL are just for contextual tips for the user & for SEO purposes... Anyways the question is how, not why :)
Seán O'Grady
<categories><url><entry-type>
How do I add a subcategory to the URL slug for a channel entry?
# How do I add a subcategory to the URL slug for a channel entry?

(Asked by: username_0 on 2014-06-20)

I'm trying to mimic the URL structure exactly how I have it in my old CMS. If I select an entry to have both a category and subcategory I would like to have the subcategory in the URL along with the category and the entry slug.
For Example
http://example.com/cat1/subcat1/entry

Right now I have the Entry URL Format set to be {category.first().slug}/{slug}, but that only shows the first category. How do I access that optional subcat1 subcategory in that field?
EDIT
I have tried the following but it doesn't work
{category.first.slug }/{ category.first.children.first.slug ? category.first.children.first.slug ~ '/' : '' }{slug}

I have also tried 
{category[0].slug}/{ category[1] ? category[1].slug ~ '/': ''}{slug}

**Tags:** categories, url, entry-type

> **Comments:**
> **username_1**: Can you try and use double curly braces {{ .. }} , if I remember correctly there was a change in Craft 2.0 which makes them necessary.  
> **username_0**: Just tried that, no difference unfortunately.  
> **username_2**: It seems that you want both of the following to load the entry template:  http://example.com/cat1/subcat1/entry &  http://example.com/cat1/entry  Is this correct?  Probably you will need to use custom routes, and a bit of template logic to determine if a request segment is a category url or an entry url.    I've not run into a need for segment 1 as a category url, it has always been something like /catalog/cat/entry or news/cat/entry/  Is there a specific reason you are not doing this way?  
> **username_0**: Because the CMS I used before – Wordpress – structures posts with subcategories in the URL in this way. To be honest it makes the most sense, otherwise what is the point of subcategories in the first place?  
> **username_2**: So .com/cat1/ would list all entries in cat1 including its subs, and .com/cat1/sub1/ would list only sub1 entries? Then you have .com/cat1/entryA/ and .com/cat1/sub1/entryA/ ?  Craft is so flexible, you can make it behave however you need.  It will help if you update your question with more example urls and what you expect to happen with each one. :)  
> **username_0**: It is a bit strange I agree, but the category urls I have set to `example.com/category/<category-name>/` The category slugs in the entry URL are just for contextual tips for the user & for SEO purposes... Anyways the question is how, not why :)  

---

## 1 answers

### Answer by username_3 on 2014-06-21. Score: 18 | Accepted answer

If you go into your Category Group’s settings and set the following settings:

Craft 2


Top Level URL Format: {slug}
Nested URL Format: {parent.uri}/{slug}

Craft 3


Category URI Format: {parent.uri}/{slug}


then you would be able to set your section’s Entry URL Format to this:

Craft 2
{myCategoryField.last.uri}/{slug}

Craft 3
{myCategoryField.inReverse().one().uri}/{slug}

> **Comments:**
> **username_4**: This is handy. Is there an easy way to just output just the child category and not include the parent?  
> **username_3**: @username_4 Not sure I’m following. Maybe post as its own question with more details?  
> **username_4**: Ok, will post up a question shortly.  
> **username_5**: In regards to the Category URI Format, if you have a prefix before all category URI's, the example given here for Craft 3 results in the prefix being duplicated. In this case the following solution worked for me: `prefix/{parent.slug}/{slug}` (using `parent.slug` rather than `parent.uri`).  
> **username_6**: @username_5 - The problem with your solution prefix/{parent.slug}/{slug} is, that its only for 2 levels. The gandparent and the grand-grand-grandparent are not sluggified with this method.  Do u know what to do in this case?  
> **username_6**: I dont really understand what this section´s Entry URL Format is.  After I changed the Category URI Format of the Category Group - I can get now 2 level URI's. Fine but what if my customer is nesting more levels.  What do u mean with section's Entry URL format? As I can see there is no section for Categories...  
> **username_3**: @username_6 This question is asking how to set _entries’_ URLs based on their selected categories. If you are just concerned with setting category URLs, you can safely ignore the part about sections (which contain entries). The category URL format above will work no matter how many levels you have.  

---

