1

I'm a newbie, so please excuse this if its a super simple question. I have a structure set up like so:

• Parent Section 1
  - Child 1A (category: pro-users)
  - Child 1B (category: pro-users)
  - Child 1c

• Parent Section 2
  - Child 2A 
  - Child 2B

I am wanting to output a navigation/landing page with the children viewable or not based on what category they are set to. So for example, on a specific page, I just want items with category "pro-users" (two entries specified above) to show up, with their parent title, and not any of the others. I also don't want the parent titles of sections that don't contain matching elements (Parent Section 2) to show. So, in this example where I just want the children with category "power-users" to show up, it would only show me:

PARENT SECTION 1
   Child 1A
   Child 1B

And nothing else. Please ask for more specifics if this doesn't make sense. I just can't figure out how to a) only show the children that are matching the categories I specify (in this case "pro-users") and to also not show the subsequent parent title if there are no matching children (as is the case for Parent Section 2).

Thanks in advance for your help, I really really appreciate it!

1 Answer 1

2

Something like this should do the trick! You want to get all the section parent entries, then search for child entries that are related to the category you want.

{% set parents = craft.entries.section('your_structure_handle').level(1) %}

{% for parent in parents %}
    {% set children = craft.entries.decendantOf(parent).relatedTo(category) %}
    {% if children|length %}
        <h1>{{parent.title}}</h1>
        <ul>
        {% for child in children %}
            <li>{{child.title}}</li>
        {% endfor %}
        </ul>
    {% endif %}
{% endfor %}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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