<p>Instead of a category, try creating each service within a structure. Then use an <code>entries</code> field to establish the relationship within the CP.</p>

CC BY-SA 3.0
2018-04-26T01:18:40.373Z
2018-04-26T01:18:40.373Z
Damon
<p>You can change the sort order in your <code>CategoryQuery</code></p>

<pre><code>{% set child = entry.getFieldValue('servies').orderBy('level DESC').one() %}
</code></pre>

<p>returns the category of the deepest level -> the child. If no child is selected you'll receive the parent</p>

CC BY-SA 3.0
2018-04-24T22:47:42.663Z
Hey Robin - thank you so much! I'm getting the child now like I'd like, but adding the `one()` will (obviously) only get me a single category. My entry could belong to several categories - some may have a child, some may not.
Damon
CC BY-SA 3.0
2018-04-24T22:53:06.437Z
That's why I never understood Pixel and Tonic decision to make Category fields the way they are. The most easiest solution would be to create a custom field type.. Copy this https://github.com/craftcms/cms/blob/develop/src/fields/Entries.php and change the word `Entry` to `Category` and you are done after you registered your new field type. Only Category fields fill the gaps https://github.com/craftcms/cms/blob/develop/src/fields/Categories.php#L91
Robin Schambach
CC BY-SA 3.0
2018-04-24T22:27:46.480Z
2018-04-24T22:27:46.480Z
Robin Schambach
<p>You could loop through the selected categories, collect their ancestors and exclude them from the final entry query.</p>
<p>This syntax is wrong but something like:</p>
<pre><code>{% set services = entry.services %}
{% set ancestors = [] %}

{% for service in services %}
  {% set ancestors = ancestors|merge(service.getAncestors()) %}
{% endfor %}

{% set mostSpecificServiceCategories = craft.categories(services).without(ancestors) %}

{% set entries = craft.entries.services(mostSpecificServiceCategories) %}
</code></pre>

CC BY-SA 4.0
2021-08-20T16:12:51.000Z
2021-08-20T16:12:51.000Z
ponies
<p>You can get the highest level of categories in your field and then filter your field only on that level.</p>

<pre><code>{% set level = 0 %}
{% for productCategory in block.productCategories.all %}
    {% set level = productCategory.level &gt; level ? productCategory.level : level %}
{% endfor %}

{% for productCategory in block.productCategories.level(level).all %}
    Do somestuff
{% endfor %}
</code></pre>

CC BY-SA 4.0
2020-02-29T07:47:20.980Z
2020-02-29T07:47:20.980Z
Kobe Aerts
<p>I have a categories field on an entry that can have one or many children. I am able to output everything - but would like to restrict the field a bit more.</p>

<p>If a child has been selected, I do not want to show the parent. In my example, I only want to show "child".</p>

<p>It is possible, that I might only choose "parent" and I only want that to show. Or, is there a different way I should go about doing this? Thank you for any suggestions!</p>

<p><a href="https://i.sstatic.net/MgvWR.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/MgvWR.jpg" alt="enter image description here"></a></p>

<p>I've tried something like this:</p>

<pre><code>{% for service in entry.servies %}
   {% set subCategories = craft.categories.descendantOf(service) %}

   {% if subCategories|length %}
       ...
   {% endif %}
{% endfor %}
</code></pre>

<p>But that will show the child category if it's selected or not. I would only like to show the "Child" category if it was actually selected. Otherwise, show the "parent" category. </p>

<p><strong>UPDATE/SOLUTION</strong></p>

<p>First, I want to thank Robin for your help. I tried different variations and was unsuccessful. I ended up taking my existing category and making it a structure. This gave me the flexibility that I needed.</p>

CC BY-SA 3.0
2018-04-24T22:09:26.347Z
I'm not sure if that works so it might be totally wrong (I can't test it now) but have you tried to order your entries by `level` and select the first? That way it might be possible to select only the deepest entry (child). Btw: if this is for Craft 3 keep in mind looping through queries is deprecated
Robin Schambach
CC BY-SA 3.0
2018-04-24T22:14:01.957Z
Yes it's Craft 3. I have a category called "Service Types". 

On my entry, I have a category field called "Services". I'm trying to only show the category that was selected. I would like to not show the parent if the child was selected.
Damon
<categories>
How can I show the child category and not it's parent - but other times show it's parent?
# How can I show the child category and not it's parent - but other times show it's parent?

(Asked by: username_0 on 2018-04-24)

I have a categories field on an entry that can have one or many children. I am able to output everything - but would like to restrict the field a bit more.
If a child has been selected, I do not want to show the parent. In my example, I only want to show "child".
It is possible, that I might only choose "parent" and I only want that to show. Or, is there a different way I should go about doing this? Thank you for any suggestions!

I've tried something like this:
{% for service in entry.servies %}
   {% set subCategories = craft.categories.descendantOf(service) %}

   {% if subCategories|length %}
       ...
   {% endif %}
{% endfor %}

But that will show the child category if it's selected or not. I would only like to show the "Child" category if it was actually selected. Otherwise, show the "parent" category. 
UPDATE/SOLUTION
First, I want to thank Robin for your help. I tried different variations and was unsuccessful. I ended up taking my existing category and making it a structure. This gave me the flexibility that I needed.

**Tags:** categories
> **Comments:**
> **username_1**: I'm not sure if that works so it might be totally wrong (I can't test it now) but have you tried to order your entries by `level` and select the first? That way it might be possible to select only the deepest entry (child). Btw: if this is for Craft 3 keep in mind looping through queries is deprecated  
> **username_0**: Yes it's Craft 3. I have a category called "Service Types".   On my entry, I have a category field called "Services". I'm trying to only show the category that was selected. I would like to not show the parent if the child was selected.  

---

## 4 answers

### Answer by username_0 on 2018-04-26. Score: 1 | Accepted answer

Instead of a category, try creating each service within a structure. Then use an `entries` field to establish the relationship within the CP.


---

### Answer by username_1 on 2018-04-24. Score: 2

You can change the sort order in your CategoryQuery
{% set child = entry.getFieldValue('servies').orderBy('level DESC').one() %}

returns the category of the deepest level -> the child. If no child is selected you'll receive the parent

> **Comments:**
> **username_0**: Hey Robin - thank you so much! I'm getting the child now like I'd like, but adding the `one()` will (obviously) only get me a single category. My entry could belong to several categories - some may have a child, some may not.  
> **username_1**: That's why I never understood Pixel and Tonic decision to make Category fields the way they are. The most easiest solution would be to create a custom field type.. Copy this https://github.com/craftcms/cms/blob/develop/src/fields/Entries.php and change the word `Entry` to `Category` and you are done after you registered your new field type. Only Category fields fill the gaps https://github.com/craftcms/cms/blob/develop/src/fields/Categories.php#L91  

---

### Answer by username_2 on 2021-08-20. Score: 1

You could loop through the selected categories, collect their ancestors and exclude them from the final entry query.
This syntax is wrong but something like:
{% set services = entry.services %}
{% set ancestors = [] %}

{% for service in services %}
  {% set ancestors = ancestors|merge(service.getAncestors()) %}
{% endfor %}

{% set mostSpecificServiceCategories = craft.categories(services).without(ancestors) %}

{% set entries = craft.entries.services(mostSpecificServiceCategories) %}


---

### Answer by username_3 on 2020-02-29. Score: 1

You can get the highest level of categories in your field and then filter your field only on that level.
{% set level = 0 %}
{% for productCategory in block.productCategories.all %}
    {% set level = productCategory.level > level ? productCategory.level : level %}
{% endfor %}

{% for productCategory in block.productCategories.level(level).all %}
    Do somestuff
{% endfor %}


---

