Answers.Body: <p>This is a common gotcha with categories, as the mechanism for relating to them is different from entries. When you relate an element to an entry, only that entry gets related. But when you relate an element to a category, <em>each of that category's parent categories becomes related to the element as well</em>. This explains why <code>categories.one()</code> returns the top parent category, because that parent is the first one related in the field.</p>
  <p><em>If your categories field is limited to a single branch</em>, a trick is to simply reverse the categories query, i.e. to return the <em>last</em> (lowest) category selected in the field:</p>
  <pre><code>{{ entry.categories.inReverse().one().url }}
  </code></pre>
  <p><strong>Note that this only works reliably when there is only a single branch selected in the categories field.</strong> If there are multiple branches, you'll probably be better off using entries (i.e. a structure section) for your &quot;categories&quot; instead of actual categories.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-12-12T08:23:31.280Z
Answers.Text: Many thanks! Your answer is very useful and I'll try it.
Answers.UserDisplayName: user2707393
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-12-10T10:52:03.573Z
Answers.LastActivityDate: 2022-12-10T10:52:03.573Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>I have the following categories</p>
  <ol>
  <li>Tech (1 entry: case A)
  1.1 Apps
  1.2 Software (2 entries: case B)</li>
  <li>Fashion
  2.1 Clothing (1 entry: case C)
  2.2 Shoes
  ...</li>
  </ol>
  <p>When using {{ entry.categories.one().url }}, it always show the parent category (Tech, Fashion). In case A, the category is Tech. In case B and C, I want to show the child categories.
  Here are the expected category URL of an entry:</p>
  <ol>
  <li>example.com/tech</li>
  <li>example.com/software</li>
  <li>example.com/clothing</li>
  </ol>
  <p>Do you have any simple way to display the final category? The final category is a parent or child one. Thanks for your answer!</p>
  
Tags: <categories>
Title: How to display the final/child category of an entry, not parent category as default
ThreadText: # How to display the final/child category of an entry, not parent category as default
  
  (Asked by: username_0 on 2022-12-09)
  
  I have the following categories
  
  1. Tech (1 entry: case A)
     1.1 Apps
     1.2 Software (2 entries: case B)
  2. Fashion
     2.1 Clothing (1 entry: case C)
     2.2 Shoes
     ...
  
  When using {{ entry.categories.one().url }}, it always show the parent category (Tech, Fashion). In case A, the category is Tech. In case B and C, I want to show the child categories.
  Here are the expected category URL of an entry:
  
  1. example.com/tech
  2. example.com/software
  3. example.com/clothing
  
  Do you have any simple way to display the final category? The final category is a parent or child one. Thanks for your answer!
  
  **Tags:** categories
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-12-10. Score: 1
  
  This is a common gotcha with categories, as the mechanism for relating to them is different from entries. When you relate an element to an entry, only that entry gets related. But when you relate an element to a category, *each of that category's parent categories becomes related to the element as well*. This explains why `categories.one()` returns the top parent category, because that parent is the first one related in the field.
  
  *If your categories field is limited to a single branch*, a trick is to simply reverse the categories query, i.e. to return the *last* (lowest) category selected in the field:
  
  ```
  {{ entry.categories.inReverse().one().url }}
  
  ```
  
  **Note that this only works reliably when there is only a single branch selected in the categories field.** If there are multiple branches, you'll probably be better off using entries (i.e. a structure section) for your "categories" instead of actual categories.
  
  > **Comments:**
  > **username_0**: Many thanks! Your answer is very useful and I'll try it.  
  
  ---
  
  