2

I have some parent and their child categories which has more than one child.

In an entry I have a category field where I selected a single child category (Bourbon) which is visible like so:

enter image description here

Now I want to show the title and URL of the only selected single child category which is Bourbon.

2

I'm not really sure why craft is working that way and I'm not really sure if my way is the intended to solve it but it works at least for me. Make sure you select the one that fits best for you. Usually the best is the third one.

First solution
If you have a limitation in your field how many categories you can link the .last() function would suit since your child is always the last element and your parent is the first one.

{% set category = entry.fieldHandle.last() %}

But this does not work if you are going to relate multiple Cateogies

Second Solution
If your selected categories are always at level 2 and never at level 1 you can do the following. This works for multiple categories as well

{% set categories = entry.fieldHandle.level(2).all() %}
{% for category in categories %}
    {{ category.id }}
    {{ category.title }}<br>
{% endfor %}

Third solution
Change your matrix structure into two different channels (countries and continent) and relate your continent with your countries. In your relation field you restrict the selection to those two groups. That way a user can select the country and/or a continent individually.

  • I haven't used this yet, I am sure this will work. Thank you so much. – Mohit Prajapati Nov 28 '17 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.