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 %}
3
  • I think I have managed to fix this, to include the missing Parent I just add in the in {{ entry.title }} Oct 2, 2017 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, 2017 at 18:06
  • 1
    Hi @BradBell, that's done - had to wait to answer my own question. Oct 3, 2017 at 8:56

1 Answer 1

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 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.