<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>

CC BY-SA 3.0
2015-05-01T14:41:31.027Z
2015-05-01T14:41:31.027Z
mjr
<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>

CC BY-SA 3.0
2015-04-29T13:23:29.877Z
Interesting! Yes, in my use case there could be two or more categories assigned at level 2 or 3.
mjr
CC BY-SA 3.0
2015-04-29T13:23:56.777Z
I wonder if it would be possible to make an array and sort by level?
mjr
CC BY-SA 4.0
2015-04-29T00:34:34.073Z
2019-05-16T20:28:49.717Z
2019-05-16T20:28:49.717Z
Douglas McDonald
<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>

CC BY-SA 4.0
2019-06-13T21:38:19.203Z
great, thank you
mjr
CC BY-SA 4.0
2019-05-16T21:41:58.480Z
2019-05-16T21:41:58.480Z
Brandon Kelly
<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>

<categories>
Getting the first category of the deepest level for an entry
# 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  

---

