<p>I don't think that there's a <a href="http://buildwithcraft.com/docs/relations" rel="nofollow">"relation"</a> between categories and their sub-categories.
But that's what you currently have as a parameter on <code>craft.categories</code>.
Use the <a href="http://buildwithcraft.com/docs/templating/craft.categories#ancestorOf" rel="nofollow"><code>ancestorOf()</code></a> parameter instead to filter your subcategories:</p>

<pre><code>{% set mainCategory = craft.categories.group('section').level(1).slug(secondSeg).first() %}
{% set secondLevelCategories = craft.categories.ancestorOf(mainCategory).slug(lastSeg) %}
</code></pre>

CC BY-SA 3.0
2014-08-14T19:46:07.940Z
2014-08-14T19:46:07.940Z
carlcs
<p>My entry url structure looks like this:</p>

<pre><code>http://site.com/section/category/subcategory/entry_slug
</code></pre>

<p>I'm trying to drill down to specific entries by using the url structure and I'm currently having trouble getting the following to work:</p>

<pre><code>{% set secondSeg = craft.request.getSegment(2) %}
{% set subCategories = craft.categories.group('section').level(2) %}

{% for cat in subCategories.relatedTo(secondSeg).slug(lastSeg) %}
    {{category.title}}
{% endfor %}
</code></pre>

<p>Currently nothing is returned. If I remove the .relatedTo(secondSeg) parameter in the cat loop, I'm properly getting all of the entries in child category that match the slug. But, the problem is that there is a possibility of certain entries having the same slug under different child categories. I'm not sure if I'm using the relatedTo parameter properly or not.</p>

<categories><filter>
How do I get entries of a child category, related to a parent category, defined in a URL segment?
# How do I get entries of a child category, related to a parent category, defined in a URL segment?

(Asked by: username_0 on 2014-08-14)

My entry url structure looks like this:
http://site.com/section/category/subcategory/entry_slug

I'm trying to drill down to specific entries by using the url structure and I'm currently having trouble getting the following to work:
{% set secondSeg = craft.request.getSegment(2) %}
{% set subCategories = craft.categories.group('section').level(2) %}

{% for cat in subCategories.relatedTo(secondSeg).slug(lastSeg) %}
    {{category.title}}
{% endfor %}

Currently nothing is returned. If I remove the .relatedTo(secondSeg) parameter in the cat loop, I'm properly getting all of the entries in child category that match the slug. But, the problem is that there is a possibility of certain entries having the same slug under different child categories. I'm not sure if I'm using the relatedTo parameter properly or not.

**Tags:** categories, filter


---

## 1 answers

### Answer by username_1 on 2014-08-14. Score: 4 | Accepted answer

I don't think that there's a "relation" between categories and their sub-categories.
But that's what you currently have as a parameter on craft.categories.
Use the ancestorOf() parameter instead to filter your subcategories:
{% set mainCategory = craft.categories.group('section').level(1).slug(secondSeg).first() %}
{% set secondLevelCategories = craft.categories.ancestorOf(mainCategory).slug(lastSeg) %}


---

