Answers.Body: <p>The URL format can be a full-blown twig template, so you can use a regular category query to retrieve the category. To replace the deprecated <code>last()</code> method, you can use <code>leaves()</code> to narrow the category query to only return leaf nodes (categories with no descendants), then use <code>one()</code> to return the first matching result:</p>
  <pre class="lang-twig prettyprint-override"><code>{{ object.categoriesField.leaves().one().uri ?? '' }}/{{ object.slug }}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-25T07:46:19.723Z
Answers.LastActivityDate: 2022-07-25T07:46:19.723Z
Answers.UserDisplayName: MoritzLost
Answers.Body: <p>Since <code>last()</code> is deprecated, here is the updated version that works:</p>
  <p><code>{categoriesField.inReverse().one().uri}/{slug}</code></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-09-26T14:58:15.097Z
Answers.LastActivityDate: 2023-09-26T14:58:15.097Z
Answers.UserDisplayName: antdev
Body: <p>I have a Category Group that can be 3 levels deep. Each category has its own page and its URI structure appends each category as you go deeper, like so:</p>
  <p><code>{parent ? parent.uri : 'root-uri' }/{slug}</code></p>
  <p>Which produces:</p>
  <p><code>/root-uri/category-1/category-2/category-3</code></p>
  <p>This all works fine.</p>
  <p>I then have a field to assign these categories to an entry. My goal is to create a URI format for the entries that include the hierarchy of the categories it's assigned to. Like:</p>
  <p><code>/root-uri/category-1/category-2/category-3/entry-slug</code></p>
  <p>or</p>
  <p><code>/root-uri/category-1/entry-slug</code></p>
  <p>What is the proper URI format for the entries to accomplish this?</p>
  <p><strong>Update</strong></p>
  <p>Using <code>{categoriesField.last().uri}/{slug}</code> is working, but the <code>.last()</code> function is deprecated.</p>
  
Tags: <entries><categories><uri>
Title: Nested categories URI format on entries
ThreadText: # Nested categories URI format on entries
  
  (Asked by: username_0 on 2022-07-22)
  
  I have a Category Group that can be 3 levels deep. Each category has its own page and its URI structure appends each category as you go deeper, like so:
  
  `{parent ? parent.uri : 'root-uri' }/{slug}`
  
  Which produces:
  
  `/root-uri/category-1/category-2/category-3`
  
  This all works fine.
  
  I then have a field to assign these categories to an entry. My goal is to create a URI format for the entries that include the hierarchy of the categories it's assigned to. Like:
  
  `/root-uri/category-1/category-2/category-3/entry-slug`
  
  or
  
  `/root-uri/category-1/entry-slug`
  
  What is the proper URI format for the entries to accomplish this?
  
  **Update**
  
  Using `{categoriesField.last().uri}/{slug}` is working, but the `.last()` function is deprecated.
  
  **Tags:** entries, categories, uri
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2022-07-25. Score: 1 | Accepted answer
  
  The URL format can be a full-blown twig template, so you can use a regular category query to retrieve the category. To replace the deprecated `last()` method, you can use `leaves()` to narrow the category query to only return leaf nodes (categories with no descendants), then use `one()` to return the first matching result:
  
  ```twig
  {{ object.categoriesField.leaves().one().uri ?? '' }}/{{ object.slug }}
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2023-09-26. Score: 1
  
  Since `last()` is deprecated, here is the updated version that works:
  
  `{categoriesField.inReverse().one().uri}/{slug}`
  
  
  ---
  
  