<p>Added a conditional to remove navigation if mainEntry is disabled.</p>

<pre><code>{% extends '_layouts/main' %}

{% block main %}

{% set mainEntry = craft.entries.slug(craft.app.request.getSegment(1)).one() %}

{% if mainEntry %}  
{% set academyLink = craft.entries.descendantOf(mainEntry).slug('academy').one() %}
{% set navLink = craft.entries.descendantOf(mainEntry).mainLevelNavigation('1').all() %}
{% set registerLink = mainEntry.registerLink %}
{% set subnav = craft.entries.descendantOf(mainEntry).secondLevelNavigation('1').all() %}
{% set subnav2 = craft.entries.descendantOf(academyLink).level(3).all() %}


&lt;nav class="navbar navbar-expand-lg main-menu"&gt;
    &lt;div class="standard-wrapper"&gt;
        &lt;div class="navbar-brand logo logo-white visible"&gt;&lt;a href="https://conference.ungerboeck.com"&gt;&lt;img src="{{siteUrl}}images/logos/UUGC_2019_year-logo_W.png"/&gt;&lt;/a&gt;&lt;/div&gt;
        &lt;!--&lt;div class="navbar-brand logo logo-black hidden"&gt;&lt;a href="https://conference.ungerboeck.com"&gt;&lt;img src="{{siteUrl}}images/logos/unite-generic-black-1.png"/&gt;&lt;/a&gt;&lt;/div&gt;--&gt;
        &lt;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)"&gt;
            &lt;span class="navbar-toggler-icon"&gt;
            &lt;div class="bar1"&gt;&lt;/div&gt;
            &lt;div class="bar2"&gt;&lt;/div&gt;
            &lt;div class="bar3"&gt;&lt;/div&gt;&lt;/span&gt;
        &lt;/button&gt;

        &lt;div class="collapse navbar-collapse float-right menu" id="navbarSupportedContent"&gt;
            &lt;ul class="navbar-nav mr-auto no-list-style"&gt;
                &lt;li class="nav-item active"&gt;
                    &lt;a class="" href="{{mainEntry.getUrl()}}"&gt;Home&lt;/a&gt;
                &lt;/li&gt;
                &lt;li class="nav-item dropdown"&gt;
                    &lt;a class="dropdown-toggle" href="javascript:void(0);" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"&gt;
                        Conference
                    &lt;/a&gt;&lt;i class="dropdown-toggle fa fa-chevron-down" data-toggle="dropdown"&gt;&lt;/i&gt;
                    &lt;div class="dropdown-menu sub-menu" aria-labelledby="navbarDropdown"&gt;
                        &lt;ul class="no-list-style"&gt;
                            {% for page in subnav %}
                            &lt;li class="nav-item"&gt;
                                {{ page.link }}
                            &lt;/li&gt;
                            {% endfor %}
                        &lt;/ul&gt;
                    &lt;/div&gt;
                &lt;/li&gt;
                {% if academyLink|length %}
                &lt;li class="nav-item dropdown"&gt;&lt;a class="dropdown-toggle" href="{{academyLink.getUrl}}" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"&gt;{{academyLink}}&lt;/a&gt;&lt;i class="dropdown-toggle fa fa-chevron-down" data-toggle="dropdown"&gt;&lt;/i&gt;
                    &lt;div class="dropdown-menu sub-menu" aria-labelledby="navbarDropdown"&gt;
                        &lt;ul class="no-list-style"&gt;
                            {% for page in subnav2 %}
                            &lt;li class="nav-item"&gt;
                                {{ page.link }}
                            &lt;/li&gt;
                            {% endfor %}
                        &lt;/ul&gt;
                    &lt;/div&gt;
                &lt;/li&gt;
                {% endif %}
                {% for page in navLink %}
                    &lt;li class="nav-item"&gt;
                        {{ page.link }}
                    &lt;/li&gt;
                {% endfor %}
                {% if registerLink|length %}
                &lt;li class="nav-item"&gt;&lt;a class="nav-link" href="{{registerLink}}"&gt;REGISTER&lt;/a&gt;&lt;/li&gt;
                {% endif %}
            &lt;/ul&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/nav&gt;

{% endif %}

{% include "conferences/_types/" ~ entry.type %}

{% endblock %}
</code></pre>

CC BY-SA 4.0
2019-07-22T21:55:57.173Z
2019-07-22T21:55:57.173Z
user9494
<p>I have an entry that needs to be disabled until the content is finalized. I am not able to 'preview' the entry unless I enable it. If I remove the navigation code, 'preview' will work on the disabled entry. Any ideas on why this is happening?
The Twig Error I am getting is Impossible to access an attribute("registerLink") on a null variable. </p>

<p><a href="https://i.sstatic.net/2BLsY.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/2BLsY.png" alt="Twig Error Image"></a></p>

<p>Here is our navigation code:</p>

<pre><code>{% extends '_layouts/main' %}

{% block main %}

{% set mainEntry = craft.entries.slug(craft.app.request.getSegment(1)).one() %}
{% set academyLink = craft.entries.descendantOf(mainEntry).slug('academy').one() %}
{% set navLink = craft.entries.descendantOf(mainEntry).mainLevelNavigation('1').all() %}
{% set registerLink = mainEntry.registerLink %}
{% set subnav = craft.entries.descendantOf(mainEntry).secondLevelNavigation('1').all() %}
{% set subnav2 = craft.entries.descendantOf(academyLink).level(3).all() %}

&lt;nav class="navbar navbar-expand-lg main-menu"&gt;
    &lt;div class="standard-wrapper"&gt;
        &lt;div class="navbar-brand logo logo-white visible"&gt;&lt;a href="#"&gt;&lt;img src="{{siteUrl}}images/logos/UUGC_2019_year-logo_W.png"/&gt;&lt;/a&gt;&lt;/div&gt;
        &lt;!--&lt;div class="navbar-brand logo logo-black hidden"&gt;&lt;a href="#"&gt;&lt;img src="{{siteUrl}}images/logos/unite-generic-black-1.png"/&gt;&lt;/a&gt;&lt;/div&gt;--&gt;
        &lt;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)"&gt;
            &lt;span class="navbar-toggler-icon"&gt;
            &lt;div class="bar1"&gt;&lt;/div&gt;
            &lt;div class="bar2"&gt;&lt;/div&gt;
            &lt;div class="bar3"&gt;&lt;/div&gt;&lt;/span&gt;
        &lt;/button&gt;

        &lt;div class="collapse navbar-collapse float-right menu" id="navbarSupportedContent"&gt;
            &lt;ul class="navbar-nav mr-auto no-list-style"&gt;
                &lt;li class="nav-item active"&gt;
                    &lt;a class="" href="{{mainEntry.getUrl}}"&gt;Home&lt;/a&gt;
                &lt;/li&gt;
                &lt;li class="nav-item dropdown"&gt;
                    &lt;a class="dropdown-toggle" href="javascript:void(0);" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"&gt;
                        Conference
                    &lt;/a&gt;&lt;i class="dropdown-toggle fa fa-chevron-down" data-toggle="dropdown"&gt;&lt;/i&gt;
                    &lt;div class="dropdown-menu sub-menu" aria-labelledby="navbarDropdown"&gt;
                        &lt;ul class="no-list-style"&gt;
                            {% for page in subnav %}
                            &lt;li class="nav-item"&gt;
                                {{ page.link }}
                            &lt;/li&gt;
                            {% endfor %}
                        &lt;/ul&gt;
                    &lt;/div&gt;
                &lt;/li&gt;
                {% if academyLink|length %}
                &lt;li class="nav-item dropdown"&gt;&lt;a class="dropdown-toggle" href="{{academyLink.getUrl}}" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"&gt;{{academyLink}}&lt;/a&gt;&lt;i class="dropdown-toggle fa fa-chevron-down" data-toggle="dropdown"&gt;&lt;/i&gt;
                    &lt;div class="dropdown-menu sub-menu" aria-labelledby="navbarDropdown"&gt;
                        &lt;ul class="no-list-style"&gt;
                            {% for page in subnav2 %}
                            &lt;li class="nav-item"&gt;
                                {{ page.link }}
                            &lt;/li&gt;
                            {% endfor %}
                        &lt;/ul&gt;
                    &lt;/div&gt;
                &lt;/li&gt;
                {% endif %}
                {% for page in navLink %}
                    &lt;li class="nav-item"&gt;
                        {{ page.link }}
                    &lt;/li&gt;
                {% endfor %}
                {% if registerLink|length %}
                &lt;li class="nav-item"&gt;&lt;a class="nav-link" href="{{registerLink}}"&gt;REGISTER&lt;/a&gt;&lt;/li&gt;
                {% endif %}
            &lt;/ul&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/nav&gt;

{% include "conferences/_types/" ~ entry.type %}

{% endblock %}
</code></pre>

<templating><live-preview>
Navigation is throwing Twig error when trying to 'preview' a disabled entry
# Navigation is throwing Twig error when trying to 'preview' a disabled entry

(Asked by: username_0 on 2019-07-16)

I have an entry that needs to be disabled until the content is finalized. I am not able to 'preview' the entry unless I enable it. If I remove the navigation code, 'preview' will work on the disabled entry. Any ideas on why this is happening?
The Twig Error I am getting is Impossible to access an attribute("registerLink") on a null variable. 

Here is our navigation code:
{% extends '_layouts/main' %}

{% block main %}

{% set mainEntry = craft.entries.slug(craft.app.request.getSegment(1)).one() %}
{% set academyLink = craft.entries.descendantOf(mainEntry).slug('academy').one() %}
{% set navLink = craft.entries.descendantOf(mainEntry).mainLevelNavigation('1').all() %}
{% set registerLink = mainEntry.registerLink %}
{% set subnav = craft.entries.descendantOf(mainEntry).secondLevelNavigation('1').all() %}
{% set subnav2 = craft.entries.descendantOf(academyLink).level(3).all() %}


    
        
        
        
            
            
            
            
        

        
            
                
                    Home
                
                
                    
                        Conference
                    
                    
                        
                            {% for page in subnav %}
                            
                                {{ page.link }}
                            
                            {% endfor %}
                        
                    
                
                {% if academyLink|length %}
                {{academyLink}}
                    
                        
                            {% for page in subnav2 %}
                            
                                {{ page.link }}
                            
                            {% endfor %}
                        
                    
                
                {% endif %}
                {% for page in navLink %}
                    
                        {{ page.link }}
                    
                {% endfor %}
                {% if registerLink|length %}
                REGISTER
                {% endif %}
            
        
    


{% include "conferences/_types/" ~ entry.type %}

{% endblock %}

**Tags:** templating, live-preview


---

## 1 answers

### Answer by username_0 on 2019-07-22. Score: 1 | Accepted answer

Added a conditional to remove navigation if mainEntry is disabled.
{% extends '_layouts/main' %}

{% block main %}

{% set mainEntry = craft.entries.slug(craft.app.request.getSegment(1)).one() %}

{% if mainEntry %}  
{% set academyLink = craft.entries.descendantOf(mainEntry).slug('academy').one() %}
{% set navLink = craft.entries.descendantOf(mainEntry).mainLevelNavigation('1').all() %}
{% set registerLink = mainEntry.registerLink %}
{% set subnav = craft.entries.descendantOf(mainEntry).secondLevelNavigation('1').all() %}
{% set subnav2 = craft.entries.descendantOf(academyLink).level(3).all() %}



    
        
        
        
            
            
            
            
        

        
            
                
                    Home
                
                
                    
                        Conference
                    
                    
                        
                            {% for page in subnav %}
                            
                                {{ page.link }}
                            
                            {% endfor %}
                        
                    
                
                {% if academyLink|length %}
                {{academyLink}}
                    
                        
                            {% for page in subnav2 %}
                            
                                {{ page.link }}
                            
                            {% endfor %}
                        
                    
                
                {% endif %}
                {% for page in navLink %}
                    
                        {{ page.link }}
                    
                {% endfor %}
                {% if registerLink|length %}
                REGISTER
                {% endif %}
            
        
    


{% endif %}

{% include "conferences/_types/" ~ entry.type %}

{% endblock %}


---

