2

I have a navigation structure as follows:

{% set page = entry ?? category ?? null %}

<ul class="nav menu--main">
    {% nav entry in entries %}

        <li class="menu__item{% if page and page.id == entry.id %} is-current{% endif %}">

            <a {% if entry.type == 'noLink' %}href=""{% else %}href="{{ entry.url }}"{% endif %}>{{ entry.title }}</a>

            {% ifchildren %}
                <ul class="nav menu--submenu__wrapper">
                    {% children %}
                </ul>
            {% endifchildren %}

        </li>

    {% endnav %}
</ul>

As you can see, I have an entry.type called "No link" which means that the user doesn't want the parent page to have a page of it's own, yet it still needs to appear in the nav to give context to the children.

My question is; how can I get the first child's URL within the ifchildren tags so I can make the parent automatically redirect to the first child's page in the navigation?

I know how to do this in regular forloops, but it would be nice to do this within Craft's nav tags.

Thanks, Jay

2

In that case you can put this in the href value:

{{ entry.descendants.type('page').first.url ?? '' }}

(Replace page with the other entry type handle(s).)

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.