I am missing something very obvious I think. I am trying to get all child categories based on url segment.

For example: "mysite.com/foo"

I have a category Group called Sections Within Sections I have a category called Foo.


Sections  <-- Category Group
    |- Foo
        |- One
        |- Two
        |- Three

Using this url as an example: www.mysite.com/foo


{% set categoryGroup = craft.categories.group('Sections').level(1).slug(craft.request.getSegment(1)).first() %}
{% set categories = craft.categories.ancestorOf(categoryGroup) %}

Doing this: <p>Group: {{ categoryGroup }}</p>

Renders this: Group: Foo


{% if categories|length %}
    {% for cat in categories %}
        {{ cat.title }}
    {% endfor %}
{% else %}
{% endif %}

I am always getting back my NOTHING statement. When I navigate to mysite.com/foo, I would like to show One Two Three

1 Answer 1


There are a couple of things I would do differently here. The first is to check the Categories in this group have their own URLs box in the Category Group settings. Set the url to {slug}, and you will have category defined in the template without needing:

{% set categoryGroup = craft.categories.group('Sections') .level(1) .slug(craft.request.getSegment(1)) .first() %}

The second problem is that once you have your category, what you want are the children, not the ancestors:

{% set theChildren = category.children %}

{% if theChildren|length %}
  {% for cat in theChildren %}
    {{ cat.title }}
  {% endfor %}
{% else %}
{% endif %}
  • Worked great - once again I was over-complicating things. Thank you!
    – Damon
    Feb 17, 2016 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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