1

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.

Categories:

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

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

twig

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

Great!

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

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

2

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 %}
  NOTHING
{% endif %}
  • Worked great - once again I was over-complicating things. Thank you! – Damon Feb 17 '16 at 15:12

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.