Answers.Body: <p>I think the best approach for the time being is the code sample in the question. Always open to explore new ideas in the future.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-01T14:41:31.027Z
Answers.LastActivityDate: 2015-05-01T14:41:31.027Z
Answers.UserDisplayName: mjr
Answers.Body: <p>Assuming you are only assigning one category (and thus by default all of that categories ancestors), then getting the 'last' returned category should work.</p>
  
  <pre><code>{% set category = entry.categories.last() %}
  </code></pre>
  
  <p>Otherwise, you will need some logic of some kind. What you have works, or perhaps something where you don't need to know how many levels there are.</p>
  
  <pre><code>{% set level = 0 %}
  {% for category in entry.categories %}
      {% set level = category.level &gt; level ? category.level : level %}
  {% endfor %}
  {% set category = entry.categories.level(level).first %}
  </code></pre>
  
  <p>However, this will still not retrieve all the categories if there is more than one category set with same level.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-29T13:23:29.877Z
Answers.Text: Interesting! Yes, in my use case there could be two or more categories assigned at level 2 or 3.
Answers.UserDisplayName: mjr
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-29T13:23:56.777Z
Answers.Text: I wonder if it would be possible to make an array and sort by level?
Answers.UserDisplayName: mjr
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2015-04-29T00:34:34.073Z
Answers.LastActivityDate: 2019-05-16T20:28:49.717Z
Answers.LastEditDate: 2019-05-16T20:28:49.717Z
Answers.UserDisplayName: Douglas McDonald
Answers.Body: <p>You could sort by <code>level</code> descending (which will order by deepest-to-shallowest), and then <code>lft</code> ascending (which will order by top-to-bottom, for entries at the same level).</p>
  
  <p>
  
  <pre><code>{% set category = craft.categories()
      .group('myGroup')
      .orderBy('level DESC, lft ASC')
      .one() %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-13T21:38:19.203Z
Answers.Text: great, thank you
Answers.UserDisplayName: mjr
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-16T21:41:58.480Z
Answers.LastActivityDate: 2019-05-16T21:41:58.480Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I want to get the first category of the deepest level for an entry.</p>
  
  <p>For example if the category structure looks like this</p>
  
  <p>Ocean Life > Fish > Gold Fish</p>
  
  <p>I want "Gold Fish"</p>
  
  <p>Here is what I've come up with so far. Is there a more elegant solution that I'm missing?</p>
  
  <pre><code>            {% set primaryCategory = entry.categories.level(3).first %}
              {% if primaryCategory is empty %}
                  {% set primaryCategory = entry.categories.level(2).first %}
              {% endif%}
              {% if primaryCategory is empty %}
                  {% set primaryCategory = entry.categories.level(1).first %}
              {% endif%}
  </code></pre>
  
Tags: <categories>
Title: Getting the first category of the deepest level for an entry
ThreadText: # Getting the first category of the deepest level for an entry
  
  (Asked by: username_0 on 2015-04-28)
  
  I want to get the first category of the deepest level for an entry.
  
  For example if the category structure looks like this
  
  Ocean Life > Fish > Gold Fish
  
  I want "Gold Fish"
  
  Here is what I've come up with so far. Is there a more elegant solution that I'm missing?
  
  ```
              {% set primaryCategory = entry.categories.level(3).first %}
              {% if primaryCategory is empty %}
                  {% set primaryCategory = entry.categories.level(2).first %}
              {% endif%}
              {% if primaryCategory is empty %}
                  {% set primaryCategory = entry.categories.level(1).first %}
              {% endif%}
  
  ```
  
  **Tags:** categories
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_0 on 2015-05-01. Score: 0 | Accepted answer
  
  I think the best approach for the time being is the code sample in the question. Always open to explore new ideas in the future.
  
  
  ---
  
  ### Answer by username_1 on 2015-04-29. Score: 4
  
  Assuming you are only assigning one category (and thus by default all of that categories ancestors), then getting the 'last' returned category should work.
  
  ```
  {% set category = entry.categories.last() %}
  
  ```
  
  Otherwise, you will need some logic of some kind. What you have works, or perhaps something where you don't need to know how many levels there are.
  
  ```
  {% set level = 0 %}
  {% for category in entry.categories %}
      {% set level = category.level > level ? category.level : level %}
  {% endfor %}
  {% set category = entry.categories.level(level).first %}
  
  ```
  
  However, this will still not retrieve all the categories if there is more than one category set with same level.
  
  > **Comments:**
  > **username_0**: Interesting! Yes, in my use case there could be two or more categories assigned at level 2 or 3.  
  > **username_0**: I wonder if it would be possible to make an array and sort by level?  
  
  ---
  
  ### Answer by username_2 on 2019-05-16. Score: 1
  
  You could sort by `level` descending (which will order by deepest-to-shallowest), and then `lft` ascending (which will order by top-to-bottom, for entries at the same level).
  
  ```
  {% set category = craft.categories()
      .group('myGroup')
      .orderBy('level DESC, lft ASC')
      .one() %}
  
  ```
  
  > **Comments:**
  > **username_0**: great, thank you  
  
  ---
  
  