Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-22T21:55:57.173Z
Answers.LastActivityDate: 2019-07-22T21:55:57.173Z
Answers.UserDisplayName: user9494
Body: <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>
  
Tags: <templating><live-preview>
Title: Navigation is throwing Twig error when trying to 'preview' a disabled entry
ThreadText: # 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.
  
  [![Twig Error Image](https://i.sstatic.net/2BLsY.png)](https://i.sstatic.net/2BLsY.png)
  
  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() %}
  
  <nav class="navbar navbar-expand-lg main-menu">
      <div class="standard-wrapper">
          <div class="navbar-brand logo logo-white visible"><a href="#"><img src="{{siteUrl}}images/logos/UUGC_2019_year-logo_W.png"/></a></div>
          <!--<div class="navbar-brand logo logo-black hidden"><a href="#"><img src="{{siteUrl}}images/logos/unite-generic-black-1.png"/></a></div>-->
          <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">
                      <a class="" href="{{mainEntry.getUrl}}">Home</a>
                  </li>
                  <li class="nav-item dropdown">
                      <a class="dropdown-toggle" href="javascript:void(0);" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                          Conference
                      </a><i class="dropdown-toggle fa fa-chevron-down" data-toggle="dropdown"></i>
                      <div class="dropdown-menu sub-menu" aria-labelledby="navbarDropdown">
                          <ul class="no-list-style">
                              {% for page in subnav %}
                              <li class="nav-item">
                                  {{ page.link }}
                              </li>
                              {% endfor %}
                          </ul>
                      </div>
                  </li>
                  {% if academyLink|length %}
                  <li class="nav-item dropdown"><a class="dropdown-toggle" href="{{academyLink.getUrl}}" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{academyLink}}</a><i class="dropdown-toggle fa fa-chevron-down" data-toggle="dropdown"></i>
                      <div class="dropdown-menu sub-menu" aria-labelledby="navbarDropdown">
                          <ul class="no-list-style">
                              {% for page in subnav2 %}
                              <li class="nav-item">
                                  {{ page.link }}
                              </li>
                              {% endfor %}
                          </ul>
                      </div>
                  </li>
                  {% endif %}
                  {% for page in navLink %}
                      <li class="nav-item">
                          {{ page.link }}
                      </li>
                  {% endfor %}
                  {% if registerLink|length %}
                  <li class="nav-item"><a class="nav-link" href="{{registerLink}}">REGISTER</a></li>
                  {% endif %}
              </ul>
          </div>
      </div>
  </nav>
  
  {% 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() %}
  
  <nav class="navbar navbar-expand-lg main-menu">
      <div class="standard-wrapper">
          <div class="navbar-brand logo logo-white visible"><a href="https://conference.ungerboeck.com"><img src="{{siteUrl}}images/logos/UUGC_2019_year-logo_W.png"/></a></div>
          <!--<div class="navbar-brand logo logo-black hidden"><a href="https://conference.ungerboeck.com"><img src="{{siteUrl}}images/logos/unite-generic-black-1.png"/></a></div>-->
          <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">
                      <a class="" href="{{mainEntry.getUrl()}}">Home</a>
                  </li>
                  <li class="nav-item dropdown">
                      <a class="dropdown-toggle" href="javascript:void(0);" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                          Conference
                      </a><i class="dropdown-toggle fa fa-chevron-down" data-toggle="dropdown"></i>
                      <div class="dropdown-menu sub-menu" aria-labelledby="navbarDropdown">
                          <ul class="no-list-style">
                              {% for page in subnav %}
                              <li class="nav-item">
                                  {{ page.link }}
                              </li>
                              {% endfor %}
                          </ul>
                      </div>
                  </li>
                  {% if academyLink|length %}
                  <li class="nav-item dropdown"><a class="dropdown-toggle" href="{{academyLink.getUrl}}" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{academyLink}}</a><i class="dropdown-toggle fa fa-chevron-down" data-toggle="dropdown"></i>
                      <div class="dropdown-menu sub-menu" aria-labelledby="navbarDropdown">
                          <ul class="no-list-style">
                              {% for page in subnav2 %}
                              <li class="nav-item">
                                  {{ page.link }}
                              </li>
                              {% endfor %}
                          </ul>
                      </div>
                  </li>
                  {% endif %}
                  {% for page in navLink %}
                      <li class="nav-item">
                          {{ page.link }}
                      </li>
                  {% endfor %}
                  {% if registerLink|length %}
                  <li class="nav-item"><a class="nav-link" href="{{registerLink}}">REGISTER</a></li>
                  {% endif %}
              </ul>
          </div>
      </div>
  </nav>
  
  {% endif %}
  
  {% include "conferences/_types/" ~ entry.type %}
  
  {% endblock %}
  
  ```
  
  
  ---
  
  