1

I am trying to display a list of entries. Specifically the parent entry and it's children's entry.

I started off using this:

{% nav page in craft.entries.section('forms').isChildOf.slug('applicant-form') %}

    <li>
        {{ page.getLink() }}

        {% ifchildren %}
            <ul>{% children %}</ul>
        {% endifchildren %}
    </li>

{% endnav %}

But this only displays the Parent.

I found this solution on Stack here, and all the children display but not the parent. I also wonder if this is the best solution to display the children as it seems a bit more lines than what is in the Craft docs.

Many thanks.

{% set topLevel = (entry.level == 1) ? entry : entry.getAncestors().level(1).first()  %}

{% if topLevel.hasDescendants %}

    <ul>
        {% set nav =  topLevel.getDescendants()  %}

        {% nav page in nav  %}
            {% if page.id != entry.id %}
                <li>{{ page.getLink() }}</li>
            {%else%}
                <li>{{page}}</li>
            {%endif%}

        {% ifchildren %}
            <ul>
                {% children %}
            </ul>
        {% endifchildren %}

        {% endnav %}

    </ul>

{% endif %}
  • I think I have managed to fix this, to include the missing Parent I just add in the in {{ entry.title }} – Steve Davies Oct 2 '17 at 14:26
  • Would you mind adding what worked for you as an official answer? Might end up helping someone in the future! :) – Brad Bell Oct 2 '17 at 18:06
  • 1
    Hi @BradBell, that's done - had to wait to answer my own question. – Steve Davies Oct 3 '17 at 8:56
1

This is the corrected Twig code. I simply added {{ entry.title }} as an <li> item. This nav appears only on the page that I needed entry.title displaying.

{% set navList = (entry.level == 2) ? entry : entry.getAncestors().level(2).first()  %}

{% if navList.hasDescendants %}

    <ul>
        {% set nav =  navList.getDescendants()  %}
        <li>{{ entry.title }}</li>
        {% nav page in nav  %}
            {% if page.id != entry.id %}
                <li>{{ page.getLink() }}</li>
            {% else %}
                <li>{{page}}</li>
            {% endif %}
        {% ifchildren %}
            <ul>
                {% children %}
            </ul>
        {% endifchildren %}
        {% endnav %}
    </ul>

{% endif %}

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.