<p>A Categories field can be related to several categories. So you'll either need to limit your Category field to a single Category relationship or you'll need to make some assumption in how you build your URL as to which category will appear in the URL. The simplest assumption to make will be that you will always return the first category for your URL.</p>

<p>Entry URL Format in the Section Settings:</p>

<pre><code>gallery/{categories.one().slug}/{slug}
</code></pre>

<p>Entry Page URL:</p>

<pre><code>http://mysite.com/gallery/category-slug/entry-slug
</code></pre>

<p>In the Entry URL Format setting above, you are telling your slug to return the <code>slug</code> of the first item in your array of categories using this line: <code>{categories.first().slug}</code> where <code>categories</code> is the handle of your Categories Relations field.</p>

<p>You may also want to ensure your Categories Relations field is required.</p>

CC BY-SA 4.0
2018-11-06T22:00:08.563Z
For Craft 3, the Entry URL Format should use the new ElementQuery method `.one()`, as: `gallery/{categories.one().slug}/{slug}`
August Miller
CC BY-SA 4.0
2018-11-07T06:12:11.487Z
Updated to Craft 3 syntax. We'll let your comment serve as the Craft 2 syntax example!
Ben Parizek
CC BY-SA 4.0
2015-08-10T10:28:47.163Z
2018-11-07T06:11:42.313Z
2018-11-07T06:11:42.313Z
Ben Parizek
<p>Should be I think:</p>

<pre><code>Entry URL Format in the Section Settings:
gallery/{category.first.slug}/{slug}
</code></pre>

CC BY-SA 4.0
2018-11-06T21:05:04.550Z
2018-11-06T21:05:04.550Z
tbfnd
<p>I have a gallery which is categorised. The gallery is a Structure section where the user adds an entry and then assigns it to a category - all good.</p>

<p>At the moment, when clicking on an entry title the URL is <em>mysite.com/gallery/entry-slug</em>  but I would like it to be <em>mysite.com/gallery/<strong>category-name</strong>/entry-slug</em>.</p>

<p>I've looked around the Docs and StackExchange for a simple answer (assuming it would just be the addition of a tag) but the solutions seem very convoluted and way outside my grasp of understanding.</p>

<p>Any help appreciated</p>

<p>Thanks</p>

<categories><url><tags><category><siteurl>
Get the Category name into the URL for an entry
# Get the Category name into the URL for an entry

(Asked by: username_0 on 2015-08-10)

I have a gallery which is categorised. The gallery is a Structure section where the user adds an entry and then assigns it to a category - all good.

At the moment, when clicking on an entry title the URL is *mysite.com/gallery/entry-slug* but I would like it to be *mysite.com/gallery/**category-name**/entry-slug*.

I've looked around the Docs and StackExchange for a simple answer (assuming it would just be the addition of a tag) but the solutions seem very convoluted and way outside my grasp of understanding.

Any help appreciated

Thanks

**Tags:** categories, url, tags, category, siteurl

---

## 2 answers

### Answer by username_1 on 2015-08-10. Score: 6

A Categories field can be related to several categories. So you'll either need to limit your Category field to a single Category relationship or you'll need to make some assumption in how you build your URL as to which category will appear in the URL. The simplest assumption to make will be that you will always return the first category for your URL.
Entry URL Format in the Section Settings:
gallery/{categories.one().slug}/{slug}

Entry Page URL:
http://mysite.com/gallery/category-slug/entry-slug

In the Entry URL Format setting above, you are telling your slug to return the slug of the first item in your array of categories using this line: {categories.first().slug} where categories is the handle of your Categories Relations field.
You may also want to ensure your Categories Relations field is required.

> **Comments:**
> **username_2**: For Craft 3, the Entry URL Format should use the new ElementQuery method `.one()`, as: `gallery/{categories.one().slug}/{slug}`  
> **username_1**: Updated to Craft 3 syntax. We'll let your comment serve as the Craft 2 syntax example!  

---

### Answer by username_3 on 2018-11-06. Score: 1

Should be I think:
Entry URL Format in the Section Settings:
gallery/{category.first.slug}/{slug}


---

