1

Trying to output navigation:

  • Home
  • About
  • Services
    • Sub 1
    • Sub 2
    • Sub 3
  • List item
  • Testimonials
  • Contact
  • Get Quote

I'd like to remove the link to the Services page, preferably to <a href"#"> and if accessed through url, redirect to homepage.

Here's how I'm currently outputting nav:

        {# get menu items #}
        {% set pageItems = craft.entries.section('services').level('<= 2') %}
        {# % set pageItems = craft.entries.level('<= 2') %# }

        {# Output the sub menu #}
        <ul>
            <li><a href="{{ siteUrl }}">Home</a></li>
            {% set entry = craft.entries.slug('about').first %}
            <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>

            {% nav page in pageItems %}
            <li>
                <a href="{{ page.navigationUrlOverride ? page.navigationUrlOverride : page.url }}">
                    {% if page.navigationTitle %}{{ page.navigationTitle|nl2br }}{% else %}{{ page.title }}{% endif %}
                </a>
                {% ifchildren %}
                <div class="drop">
                    <ul>
                        {% children %}
                    </ul>
                </div>
                {% endifchildren %}
            </li>
            {% endnav %}
        </ul>
        <ul>
            {% set entry = craft.entries.slug('testimonials').first %}
                <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
            {% set entry = craft.entries.slug('contact').first %}
                <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
            {% set entry = craft.entries.slug('get-a-quote').first %}
                <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
        </ul>

Any help is much appreciated. Thanks in advance.

  • Your code does actually look good to me? You use a plain text field navigationUrlOverride to overwrite the URL with # from the CP, right? – carlcs Dec 9 '14 at 9:21
  • I tried that, but it adds # to all children. Want the children links to work. – frshjb373 Dec 9 '14 at 20:55
  • Sounds like a bug to me. You could work around this by replacing the nav tag with nested for loops. – carlcs Dec 9 '14 at 21:07
5

There are two very different questions here, but I will try my best to answer them both!

The first being "How do I hide a page from the navigation?"

The simplest way I can think of for this, that is also controllable through Craft, is to create a simple "Hide from navigation" Lightswitch field, and add this to your entry types.

When looping through entries for the navigation, you can check whether the lightswitch has been checked, and if it has, don't show that entry in the nav:

{% for entry in craft.entries.section('lorem').hideFromNavigation(0) %}
    --
{% endfor %}

The second question is "How do I redirect to the homepage if someone visits /services?"

This can be achieved with the redirect tag. In your services/index.twig template file, you can simply add:

{% redirect siteUrl %}
  • Not trying to hide from navigation, just want to remove link from the "Services" page and/or remove page entirely and modify code above accordingly. Any ideas? – frshjb373 Dec 9 '14 at 16:19
  • Thx for the hideFromNavigation tip. Exactly what I was looking for! :-) – Mario Dec 20 '16 at 19:05
  • Had to use this syntax to exclude entries with active lightswitch: .hideFromNavigation('not 1') – Mario Dec 20 '16 at 19:24
2

You need to add a conditional to either only run for the parent items OR exclude the children items. Using depth level would do it:

So instead of:

<a href="{{ page.navigationUrlOverride ? page.navigationUrlOverride : page.url }}">

try:

{% if page.navigationUrlOverride and page.level == '1' %}
    <a href="{{ page.navigationUrlOverride }}" />
{% else %}
    <a href="{{page.url}}" />
{% endif %}
  • This doesn't allow you to override level 2 entries. – carlcs Dec 9 '14 at 22:56
  • Precisely. I kept it simple to illustrate how to work around the children adopting the value of the parent while still maintaining the use of the children tag. – Jarrett Barnett Dec 10 '14 at 0:25
2

As I wrote in the comments section, you could work around this strange behavior of the nav tag by replacing it with nested for loops. Use getDescendants(1) to get the child entries of each top-level entry.

{% for page in pageItems %}
    <li>
        <a href="{{ page.navigationUrlOverride ? page.navigationUrlOverride : page.url }}">
            {{ page.navigationTitle ? page.navigationTitle|nl2br : page.title }}
        </a>

        {% set subPageItems = page.getDescendants(1) %}
        {% if subPageItems %}
            <div class="drop">
                <ul>
                    {% for subPage in subPageItems %}
                        <a href="{{ page.navigationUrlOverride ? page.navigationUrlOverride : page.url }}">
                            {{ page.navigationTitle ? page.navigationTitle|nl2br : page.title }}
                        </a>
                    {% endfor %}
                </ul>
            </div>
        {% endif %}
    </li>
{% endfor %}
1

You can also do a simple if/else to hide the href link using a lightswitch toggle. Using Jamie's hideFromNavigation lightswitch from above, you could do the following:

{% set entries = craft.entries.section('pages') %}
<ul id="nav">
    {% nav entry in entries %}
        <li>
            <a  {% if entry.hideFromNavigation %} href="#" {% else %} href="{{ entry.url }}" {% endif %}>{{ entry.title }}</a>
            {% ifchildren %}
                <ul>
                    {% children %}
                </ul>
            {% endifchildren %}
        </li>
    {% endnav %}
</ul>

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.