My entry url structure looks like this:


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) %}
{% 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.

1 Answer 1


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

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.