1

I have an entry that acts as a parent to several children pages. Trying to make so that particular parent isn't clickable AND reroutes to homepage if address url is accessed.

Not sure if I can work something into existing nav code or if this needs to be done somewhere else, but here's the code, nonetheless. Thanks in advance.

<ul>
    {% set pages = craft.entries.section('page') %}
    {% nav page in pages %}
        <li>
            {{ page.getLink() }}
            {% ifchildren %}
                <ul>
                    {% children %}
                </ul>
            {% endifchildren %}
        </li>
    {% endnav %}
</ul>
3

You can use the level method to determine which iteration of the nav you are on and change the behavior:

<ul>
    {% set pages = craft.entries.section('page') %}
    {% nav page in pages %}
        <li>
            {% if page.level == 1 %}
                <a href="/">{{ page.title }}</a>
            {% else %}
                {{ page.getLink() }}
            {% endif %}
            {% ifchildren %}
                <ul>
                    {% children %}
                </ul>
            {% endifchildren %}
        </li>
    {% endnav %}
</ul>

of if it's only one particular entry, then you can create a conditional to check for that entry (using slug, id, title, or any other attribute or field value).

<ul>
    {% set pages = craft.entries.section('page') %}
    {% nav page in pages %}
        <li>
            {% if page.slug == "myPage" %}
                <a href="/">{{ page.title }}</a>
            {% else %}
                {{ page.getLink() }}
            {% endif %}
            {% ifchildren %}
                <ul>
                    {% children %}
                </ul>
            {% endifchildren %}
        </li>
    {% endnav %}
</ul>

If the url is accessed directly you can add a redirect to the template.

{% if entry.level == 1 %}
    {% redirect siteUrl %}
{% endif %}
| improve this answer | |
  • The second snippet of code worked. The url redirect I used entry.slug for the redirect. Thank you for the help! – frshjb373 Nov 14 '14 at 16:20

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.