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

<blockquote>
  <p><code>{% set categoryGroup = craft.categories.group('Sections')
  .level(1)
  .slug(craft.request.getSegment(1))
  .first() %}</code></p>
</blockquote>

<p>The second problem is that once you have your <a href="https://craftcms.com/docs/templating/categorymodel" rel="nofollow">category</a>, what you want are the <a href="https://craftcms.com/docs/templating/categorymodel#getChildren" rel="nofollow">children</a>, not the <a href="https://craftcms.com/docs/templating/categorymodel#getAncestors" rel="nofollow">ancestors</a>:</p>

<pre><code>{% set theChildren = category.children %}

{% if theChildren|length %}
  {% for cat in theChildren %}
    {{ cat.title }}
  {% endfor %}
{% else %}
  NOTHING
{% endif %}
</code></pre>

CC BY-SA 3.0
2016-02-17T15:12:38.177Z
Worked great - once again I was over-complicating things. Thank you!
Damon
CC BY-SA 3.0
2016-02-15T20:18:08.060Z
2016-02-15T20:18:08.060Z
Marion Newlevant
<p>I am missing something very obvious I think. I am trying to get all child categories based on url segment.</p>

<p>For example: "mysite.com/foo"</p>

<p>I have a category Group called <code>Sections</code> Within <code>Sections</code> I have a category called <code>Foo</code>.</p>

<p><strong>Categories</strong>:</p>

<pre><code>Sections  &lt;-- Category Group
    |- Foo
        |- One
        |- Two
        |- Three
</code></pre>

<p>Using this url as an example: <code>www.mysite.com/foo</code></p>

<p><strong>twig</strong></p>

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

<p>Doing this: <code>&lt;p&gt;Group: {{ categoryGroup }}&lt;/p&gt;</code> </p>

<p>Renders this: <code>Group: Foo</code></p>

<p>Great!</p>

<pre><code>{% if categories|length %}
    {% for cat in categories %}
        {{ cat.title }}
    {% endfor %}
{% else %}
    NOTHING
{% endif %}
</code></pre>

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

<categories><segments>
How can I get all child categories - of a child category based on url segment?
# How can I get all child categories - of a child category based on url segment?

(Asked by: username_0 on 2016-02-15)

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: Group: {{ categoryGroup }} 
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

**Tags:** categories, segments

---

## 1 answers

### Answer by username_1 on 2016-02-15. Score: 2 | Accepted answer

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

> **Comments:**
> **username_0**: Worked great - once again I was over-complicating things. Thank you!  

---

