2

I'm building a navigation menu with 2 levels.

I have a contact page with a subpage thank you, which is hidden.

I hide pages from the menu using a lightswitch option and filter these pages out in my entries call.

However I check if a page has subpages using hasDescendants(), and if the page does, I add the class "has-dropdown" but my contact menu item contains the class "has-dropdown" too which technically is correct as it has a subpage but is it possible to filter with hasDescendants() ?

{% set pages = craft.entries.find({section: "page", genericHideFromNavigation: "[0]"}) %}
{% if pages | length %}
    <ul class="main-nav">
        {% nav page in pages %}
            <li class="{% if page.hasDescendants() %}has-dropdown{% endif %}">
                <a href="{{ page.url }}" title="{{ page.title }}">{{ page.title }}</a>

                {% ifchildren %}
                    <ul class="dropdown">
                        {% children %}
                    </ul>
                {% endifchildren %}
            </li>
        {% endnav %}
    </ul>
{% endif %}
Brad Bell
  • 67,440
  • 6
  • 73
  • 143
Sean Delaney
  • 720
  • 6
  • 15

1 Answers1

2

What you could do is to get all the descendants and loop them to check if at least one has that lightswitch not set.

{% set showDropdown = 0 %}
{% for subPage in page.getDescendants() if subPage.genericHideFromNavigation == 0 %}
    {% set showDropdown = 1 %}
{% endif %}

<li class="{% if showDropdown %}has-dropdown{% endif %}"></li>
carlcs
  • 36,220
  • 5
  • 62
  • 139