<p>You can use <code>.all()</code> to get all selected categories in order. Since the URI format is a Twig template, you can use some Twig filters to output all selected categories as path segments:</p>
<pre class="lang-twig prettyprint-override"><code>{{ eventCategory.all()|column('slug')|join('/') }}/{{ slug }}
</code></pre>
<p>The <code>column</code> filter will extract the <code>slug</code> property from the array of entries, and <code>join</code> will join the array of slugs to a single string separated by slashes <code>/</code>.</p>
<p>This will result in <code>category1/slug</code> for an entry with only one category, and <code>category1/category2/slug</code> for entries with two categories. It will also work for any number of selected categories (though you may want a different format in this case).</p>
<p>The syntax for object templates is described in the documentation: <a href="https://craftcms.com/docs/5.x/system/object-templates.html#syntax" rel="nofollow noreferrer">https://craftcms.com/docs/5.x/system/object-templates.html#syntax</a></p>

CC BY-SA 4.0
2024-05-30T16:36:18.747Z
Looks like there is an error in your anwser, i think it should be: `{ eventCategory.all()|column('slug')|join('/') }/{ slug }`
Jan
CC BY-SA 4.0
2024-05-31T14:04:18.813Z
Good call @Jan; updated!
Jalen Davenport
CC BY-SA 4.0
2024-05-31T19:13:57.100Z
@Jan No, that's not an error, it's just Twig syntax. The single-bracket syntax is just a shorthand. I prefer to use the full syntax if I'm also using other twig features. Doesn't matter which style you prefer, but the syntax in my answer is correct and fully supported. See the documentation here: https://craftcms.com/docs/5.x/system/object-templates.html#syntax
MoritzLost
CC BY-SA 4.0
2024-05-31T19:17:33.807Z
@JalenDavenport I've reverted this edit because I wanted to use the full Twig syntax here to make it easier to understand what's going on. Let me know if you disagree! Also added the link to the new docs page on object templates for clarity.
MoritzLost
CC BY-SA 4.0
2024-05-31T22:28:04.967Z
@MoritzLost yeah fair enough, whether you use single or double brackets is just syntactic sugar. However, you were initially missing the closing bracket on your first tag, which I think is the main issue Jan was pointing out... 
Jalen Davenport
CC BY-SA 4.0
2024-06-01T14:01:23.957Z
@JalenDavenport Ah, missed that in the edit, thanks for correcting that!
MoritzLost
CC BY-SA 4.0
2023-11-17T11:09:31.627Z
2024-05-31T19:14:40.243Z
2024-05-31T19:14:40.243Z
MoritzLost
<p>i am trying to set the url format for some entries to include the category slugs set for it within a category entry field - i can get the first one like this</p>
<pre><code>{eventCategory.one().slug}/{slug}
</code></pre>
<p>but cannot figure out how to get the second one (the 2nd one is optional and is a sub-cat of category1 also so not sure how to do some optional logic for it within the 'Entry URI Format')</p>
<p>so the url could be
category1/slug OR
category1/catgeory2/slug</p>
<p>Any ideas anyone? Thanks</p>

<categories><entries-field>
Entry URI Format from an Entries Field (Categories)
# Entry URI Format from an Entries Field (Categories)

(Asked by: username_0 on 2023-11-17)

i am trying to set the url format for some entries to include the category slugs set for it within a category entry field - i can get the first one like this
{eventCategory.one().slug}/{slug}

but cannot figure out how to get the second one (the 2nd one is optional and is a sub-cat of category1 also so not sure how to do some optional logic for it within the 'Entry URI Format')
so the url could be
category1/slug OR
category1/catgeory2/slug
Any ideas anyone? Thanks

**Tags:** categories, entries-field

---

## 1 answers

### Answer by username_1 on 2023-11-17. Score: 2

You can use .all() to get all selected categories in order. Since the URI format is a Twig template, you can use some Twig filters to output all selected categories as path segments:
{{ eventCategory.all()|column('slug')|join('/') }}/{{ slug }}

The column filter will extract the slug property from the array of entries, and join will join the array of slugs to a single string separated by slashes /.
This will result in category1/slug for an entry with only one category, and category1/category2/slug for entries with two categories. It will also work for any number of selected categories (though you may want a different format in this case).
The syntax for object templates is described in the documentation: https://craftcms.com/docs/5.x/system/object-templates.html#syntax

> **Comments:**
> **username_2**: Looks like there is an error in your anwser, i think it should be: `{ eventCategory.all()|column('slug')|join('/') }/{ slug }`  
> **username_3**: Good call @Jan; updated!  
> **username_1**: @Jan No, that's not an error, it's just Twig syntax. The single-bracket syntax is just a shorthand. I prefer to use the full syntax if I'm also using other twig features. Doesn't matter which style you prefer, but the syntax in my answer is correct and fully supported. See the documentation here: https://craftcms.com/docs/5.x/system/object-templates.html#syntax  
> **username_1**: @JalenDavenport I've reverted this edit because I wanted to use the full Twig syntax here to make it easier to understand what's going on. Let me know if you disagree! Also added the link to the new docs page on object templates for clarity.  
> **username_3**: @MoritzLost yeah fair enough, whether you use single or double brackets is just syntactic sugar. However, you were initially missing the closing bracket on your first tag, which I think is the main issue Jan was pointing out...   
> **username_1**: @JalenDavenport Ah, missed that in the edit, thanks for correcting that!  

---

