1

I have a entry field populated by categories. The number of categories an entry can have is unlimited and the categories are set up as nested categories i.e.

  • first level
  • ---second level
  • ------third level

  • first level 2

  • ---second level 2
  • ------third level 2

I want to be able to list out any categories selected at the third level that the entry may have selected if a certain parent category is identified. So far I have the following code. This works but lists all child categories for the identified parent instead of just the selected ones.

What am I missing?

{% set cats = entry.categories %}
{% for cat in cats %}
    {% if cat.slug == 'secondlevel1' or cat.slug == 'secondlevel2' %}
        {% set catChildren = cats.descendantOf(cat.id).descendantDist('1') %}
        {% if catChildren | length %}
            {% for childCat in catChildren %}
                {% set image = childCat.buildingBlockIcon.first() %}
                    {% if image %}
                        <img alt="{{ childCat.title }}" src="{{ image.url }}" />
                    {% endif %}
            {% endfor %}
        {% endif %}
    {% endif %}
{% endfor %}

----- EDIT Solution -----

I worked this out

by changing one line of code line 4 above from

{% set catChildren = cat.level(2).children %}

to

{% set catChildren = cats.descendantOf(cat.id).descendantDist('1') %}

Now works a treat. Any better solutions feel free to post.

  • Give this thread a peek. I think its very similar to what you are looking for. – Damon Nov 24 '15 at 20:42
  • @damon thanks for the link. But it's kind of the opposite to what I want to achieve. I'm looking at in reverse. I have the entry, from the entry I want to obtain what third level categories have been assigned to it and then use these to link to a related entries page. At present I can only get it to list all third level categories and not just the ones that were selected for the specific entry. So for example if the third level has 4 items and the entry only has 2 selected it still outputs all 4 items instead of just the selected 2. So I need a way to identify which items were selected. – Lettie Nov 25 '15 at 10:52
  • @damon I've managed to solve it by using the descendantOf instead of level. I've edited my post with the answers above. Thanks for the clues! – Lettie Nov 25 '15 at 11:32
  • @Lettie would you mind adding the solution you found as an official answer instead of an edit to the original question? – Brad Bell Nov 25 '15 at 16:13
  • @bradbell no problem – Lettie Nov 25 '15 at 16:46
1

The final solution I got to work:

{% set cats = entry.categories %}
{% for cat in cats %}
    {% if cat.slug == 'secondlevel1' or cat.slug == 'secondlevel2' %}
        {% set catChildren = cats.descendantOf(cat.id).descendantDist('1') %}
        {% if catChildren | length %}
            {% for childCat in catChildren %}
                {% set image = childCat.buildingBlockIcon.first() %}
                    {% if image %}
                        <img alt="{{ childCat.title }}" src="{{ image.url }}" />
                    {% endif %}
            {% endfor %}
        {% endif %}
    {% endif %}
{% endfor %}

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.