2

Newbie here with hopefully a simple question...

I have a structure as per the image below where I want to only show the parent item and it's children on their pages.

I want to exclude the other entries but they should show on their own pages.

My structure

2 Answers 2

6

You can access children through the entry's descendants property.

{# display event-information #}
<ul>
    <li><a href="{{ entry.url }}">{{ entry.title }}</a>
    {# display subsections #}
        <ul>
            {% for subsection in entry.descendants %}
                <li><a href="{{ subsection.url }}">{{ subsection.title }}</a></li>
            {% endfor %}
        </ul>
    </li>
</ul>

You can also use the nav tag, as described in the documentation.

<ul id="nav">
    {% nav entry in entries %}
        <li>
            <a href="{{ entry.url }}">{{ entry.title }}</a>
            {% ifchildren %}
                <ul>
                    {% children %}
                </ul>
            {% endifchildren %}
        </li>
    {% endnav %}
</ul>

More generally speaking, you can use any of the following properties of the EntryModel to traverse the structure: parent, children, descendants, ancestors, siblings, nextSibling, prevSibling, and level.

3
  • Worth noting that the top example has a typo on line 7, the <li> element isn't closed, but rather a second <li>. Commented Apr 17, 2017 at 21:20
  • @ChristopherRobertKennedy Thanks! Fixed. Btw... feel free to edit when you find an obvious error like that, assuming you have permission. Commented Apr 19, 2017 at 22:04
  • For some reason, I was having problems accessing the edit feature in Chrome. Seems to work for me in Safari now, though. ¯_(ツ)_/¯ Commented Apr 20, 2017 at 1:11
0

Try using .level(1)

{% for eventsEntry in craft.entries.section('events').level(1) %}
    <div>
        {% set image = eventsEntry.pic.first() %}

        {% if image %}
            <img src="{{ image.getUrl('thumb') }}" alt="{{image.title}}"/>
        {% endif %}

        <h4 class="center">{{ eventsEntry.title }}</h4>

        {{ eventsEntry.shortDescription }}
    </div>
{% endfor %}

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.