1

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.

4

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, privacy policy and cookie policy

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