1

I am looking to hide the main navigation for entries that have the lightswitch 'showMainNavigation' disabled. The problem I am facing is when I disable for one entry, the navigation is hidden for all entries. Here is a sample of the code:

{% set mainEntry = craft.entries.slug(craft.app.request.getSegment(1)).one() %}
{% set menuLogo = mainEntry.menuLogo %}
{% set hideMenu = craft.entries.section('mysection').showMainNavigation('not 1').one() %} 

{% if hideMenu %}
<nav class="navbar navbar-expand-lg main-menu">
    <div class="standard-wrapper">
        {% if menuLogo|length %}
        <div class="navbar-brand logo logo-white visible"><a href="#"><img src="{{siteUrl}}{{menuLogo.one().getUrl()}}"/></a></div>
        {% endif %}
    </div>
</nav>

{% else %}

<nav class="navbar navbar-expand-lg main-menu">
    <div class="standard-wrapper">
        {% if menuLogo|length %}
        <div class="navbar-brand logo logo-white visible"><a href="#"><img src="{{siteUrl}}{{menuLogo.one().getUrl()}}"/></a></div>
        {% endif %}
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" onclick="myFunction(this)">
            <span class="navbar-toggler-icon">
            <div class="bar1"></div>
            <div class="bar2"></div>
            <div class="bar3"></div></span>
        </button>

        <div class="collapse navbar-collapse float-right menu" id="navbarSupportedContent">
            <ul class="navbar-nav mr-auto no-list-style">
                <li class="nav-item active">list item 1</li>
                <li class="nav-item active">list item 2</li>
            </ul>
         </div>
     </div>
</nav>
{% endif %}

1 Answer 1

2

This line:

{% set hideMenu = craft.entries.section('mysection').showMainNavigation('not 1').one() %} 

will return the first entry that it can find in the section 'mysection' that has showMainNavigation off. So as soon as you disable it for one entry, hideMenu will 'return' true, whichever page you're on.

So to fix this, if mainEntry is your current entry, then replace the line by:

{% set hideMenu = mainEntry.showMainNavigation %} 
1
  • 1
    Wow, I was really overthinking this! mainEntry is not the current entry but I used your example to fix this. {% set showMenu = entry.showMainNavigation %} then {% if not showMenu%}hide navigation{% else %}show navigation.
    – user9494
    Commented Nov 19, 2019 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.