Answers.Body: <p><strong>Using Navigation Plugin</strong></p>
  <p>Are you using the &quot;<a href="https://plugins.craftcms.com/navigation?craft4" rel="nofollow noreferrer">Navigation</a>&quot; plugin by Verbb?</p>
  <p>If you are, when you are entering your menu items there is an option to add &quot;New Window&quot; when you edit the menu item.</p>
  <p><a href="https://i.sstatic.net/tsjqU.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/tsjqU.png" alt="screenshot of navigation menu items" /></a></p>
  <p><a href="https://i.sstatic.net/u8NbM.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/u8NbM.png" alt="screenshot of navigation menu item edit panel" /></a></p>
  <hr />
  <p><strong>Not Using Navigation Plugin</strong></p>
  <p>Otherwise, if you aren't you could compare the the item URL to the site's. It's hard to advise without knowing how you built the navigation.</p>
  <p>If you are looking to just compare the URLs entered within the field you could do something like this:</p>
  <pre><code>&lt;a href=&quot;{{child.url}}&quot; {{ if 'siteurl.com' not in child.url ? 'target=&quot;_blank&quot;' }}&gt;{{child.title}}&lt;/a&gt;
  </code></pre>
  <p>You can adjust the above code snippet to work for a toggle or checkbox too if you set it up like that in your admin. You would just switch out <code>'siteurl.com' not in child.url</code> for <code>child.fieldname == true</code></p>
  <p>You could also set up a variable if you are going to reuse the <code>child</code> element before exiting the loop. If you wanted to set up a variable it would be something like this directly under your opening <code>for</code></p>
  <pre><code>{% set linkTarget = '' %}
  {% if 'siteurl.com' not in child.url %}
       {% set linkTarget = 'target=&quot;_blank&quot;' %}
  {% end if %}
  
  &lt;a href=&quot;{{child.url}}&quot; {{ linkTarget }}&gt;{{child.title}}&lt;/a&gt;
  </code></pre>
  <p>Note you can sub the <code>{% if 'siteurl.com' not in child.url %}</code> for <code>{% if child.fieldName == true %}</code></p>
  <hr />
  <p><strong>Closing Note</strong></p>
  <p>It would be most helpful if you could explain how you have created the navigation in the admin. I am doing a bit of guesswork at the moment. Please let me know if these options work, or if you have more information for me to review.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-11T22:41:12.200Z
Answers.LastActivityDate: 2024-01-11T22:41:12.200Z
Answers.UserDisplayName: kelly
Body: <p>I have a code that I am using to make the menu I created in Craft CMS work. In my menu, I have direct and indirect links. and I also have a child option that uses an EXTERNAL link. In other words, the person clicks on the option and instead of taking an entry to the website, they actually need to be taken to an external link. I need to add this if to my code but as I'm a beginner, I'm having a lot of difficulty. I would be grateful to anyone who could help me.</p>
  <p>the code is the following:</p>
  <pre><code>{% if menu|length %}
  &lt;nav class=&quot;side-menu m-left-side m-show-submenu&quot;&gt;
      &lt;ul&gt;
          {% for menuItem in menu %} &lt;!-- inicia um loop que percorre cada item na variável 'menu'--&gt;
              &lt;li&gt;
                  {% set links = menuItem.linkDeDestino.all() %}
                      {% if links|length &gt; 1 %}     &lt;!--  Isso verifica se há mais de um link associado ao item de menu. Se for verdadeiro, significa que há links indiretos e um submenu será criado --&gt;
                          &lt;a href=&quot;javascript:void(0)&quot;&gt;{{menuItem.tituloDaOpcao}}&lt;/a&gt; &lt;!-- cria um link para o item de menu. O href é definido como &quot;javascript:void(0)&quot; para que o link não redirecione para uma página diferente. O texto do link é obtido da propriedade 'tituloDaOpcao' do 'menuItem'. --&gt;
                          &lt;ul&gt;
                              {% for child in links %} &lt;!-- inicia um loop para cada link indireto (child) associado ao item de menu. --&gt;
                                  &lt;li&gt;
                                      &lt;a href=&quot;{{child.url}}&quot;&gt;{{child.title}}&lt;/a&gt;
  
                                      {% if child.hasDescendants %}  &lt;!-- se existir descentende indireto, ou seja, links após o children, então executa este código --&gt;
  
                                      {% set descendants = craft.entries()  
                                          .descendantOf(child)
                                          .all() %} &lt;!-- Isso define uma variável descendants que contém todos os descendentes do link indireto. --&gt;
                                      &lt;ul&gt;
                                          {% for child in descendants %} &lt;!-- Isso inicia um loop para cada descendente do link indireto. --&gt;
                                          
                                              &lt;li&gt;&lt;a href=&quot;{{child.url}}&quot;&gt;{{child.title}}&lt;/a&gt;&lt;/li&gt;
                                          
                                          {% endfor %}
                                      &lt;/ul&gt;
                                      {% endif %}
                                  &lt;/li&gt;
                              {% endfor %}
                          &lt;/ul&gt;
                      {% else %}  
  
                      {% set link =  menuItem.linkDeDestino.one() %} 
                          &lt;a href=&quot;{{link.url}}&quot;&gt;{{menuItem.tituloDaOpcao}}&lt;/a&gt;
                      {% if link.hasDescendants %}
                          {% set descendants = craft.entries()
                                          .descendantOf(link)
                                          .all() %}
                          &lt;ul&gt;
                              {% for child in descendants %}
                                  &lt;li&gt;&lt;a href=&quot;{{child.url}}&quot;&gt;{{child.title}}&lt;/a&gt;&lt;/li&gt;
                              {% endfor %}
                          &lt;/ul&gt;
                      {% endif %}
  
                  {% endif %}
  
                  
              &lt;/li&gt;
          {% endfor %}
      &lt;/ul&gt;
  &lt;/nav&gt;
  {% endif %}
  </code></pre>
  <p>I think I need an if that checks if there is an external url field in a child, if so, open the link on a new page. I really appreciate the help!!</p>
  <p>I also leave the craft menu here so you can better understand the hierarchy:</p>
  <p><a href="https://i.sstatic.net/oCKUg.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/oCKUg.jpg" alt="enter image description here" /></a></p>
  
Tags: <craft4>
Title: how to add an if to open an external link in the menu
ThreadText: # how to add an if to open an external link in the menu
  
  (Asked by: username_0 on 2023-12-06)
  
  I have a code that I am using to make the menu I created in Craft CMS work. In my menu, I have direct and indirect links. and I also have a child option that uses an EXTERNAL link. In other words, the person clicks on the option and instead of taking an entry to the website, they actually need to be taken to an external link. I need to add this if to my code but as I'm a beginner, I'm having a lot of difficulty. I would be grateful to anyone who could help me.
  
  the code is the following:
  
  ```
  {% if menu|length %}
  <nav class="side-menu m-left-side m-show-submenu">
      <ul>
          {% for menuItem in menu %} <!-- inicia um loop que percorre cada item na variável 'menu'-->
              <li>
                  {% set links = menuItem.linkDeDestino.all() %}
                      {% if links|length > 1 %}     <!--  Isso verifica se há mais de um link associado ao item de menu. Se for verdadeiro, significa que há links indiretos e um submenu será criado -->
                          <a href="javascript:void(0)">{{menuItem.tituloDaOpcao}}</a> <!-- cria um link para o item de menu. O href é definido como "javascript:void(0)" para que o link não redirecione para uma página diferente. O texto do link é obtido da propriedade 'tituloDaOpcao' do 'menuItem'. -->
                          <ul>
                              {% for child in links %} <!-- inicia um loop para cada link indireto (child) associado ao item de menu. -->
                                  <li>
                                      <a href="{{child.url}}">{{child.title}}</a>
  
                                      {% if child.hasDescendants %}  <!-- se existir descentende indireto, ou seja, links após o children, então executa este código -->
  
                                      {% set descendants = craft.entries()  
                                          .descendantOf(child)
                                          .all() %} <!-- Isso define uma variável descendants que contém todos os descendentes do link indireto. -->
                                      <ul>
                                          {% for child in descendants %} <!-- Isso inicia um loop para cada descendente do link indireto. -->
                                          
                                              <li><a href="{{child.url}}">{{child.title}}</a></li>
                                          
                                          {% endfor %}
                                      </ul>
                                      {% endif %}
                                  </li>
                              {% endfor %}
                          </ul>
                      {% else %}  
  
                      {% set link =  menuItem.linkDeDestino.one() %} 
                          <a href="{{link.url}}">{{menuItem.tituloDaOpcao}}</a>
                      {% if link.hasDescendants %}
                          {% set descendants = craft.entries()
                                          .descendantOf(link)
                                          .all() %}
                          <ul>
                              {% for child in descendants %}
                                  <li><a href="{{child.url}}">{{child.title}}</a></li>
                              {% endfor %}
                          </ul>
                      {% endif %}
  
                  {% endif %}
  
                  
              </li>
          {% endfor %}
      </ul>
  </nav>
  {% endif %}
  
  ```
  
  I think I need an if that checks if there is an external url field in a child, if so, open the link on a new page. I really appreciate the help!!
  
  I also leave the craft menu here so you can better understand the hierarchy:
  
  [![enter image description here](https://i.sstatic.net/oCKUg.jpg)](https://i.sstatic.net/oCKUg.jpg)
  
  **Tags:** craft4
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-01-11. Score: 1
  
  **Using Navigation Plugin**
  
  Are you using the "[Navigation](https://plugins.craftcms.com/navigation?craft4)" plugin by Verbb?
  
  If you are, when you are entering your menu items there is an option to add "New Window" when you edit the menu item.
  
  [![screenshot of navigation menu items](https://i.sstatic.net/tsjqU.png)](https://i.sstatic.net/tsjqU.png)
  
  [![screenshot of navigation menu item edit panel](https://i.sstatic.net/u8NbM.png)](https://i.sstatic.net/u8NbM.png)
  
  ---
  
  **Not Using Navigation Plugin**
  
  Otherwise, if you aren't you could compare the the item URL to the site's. It's hard to advise without knowing how you built the navigation.
  
  If you are looking to just compare the URLs entered within the field you could do something like this:
  
  ```
  <a href="{{child.url}}" {{ if 'siteurl.com' not in child.url ? 'target="_blank"' }}>{{child.title}}</a>
  
  ```
  
  You can adjust the above code snippet to work for a toggle or checkbox too if you set it up like that in your admin. You would just switch out `'siteurl.com' not in child.url` for `child.fieldname == true`
  
  You could also set up a variable if you are going to reuse the `child` element before exiting the loop. If you wanted to set up a variable it would be something like this directly under your opening `for`
  
  ```
  {% set linkTarget = '' %}
  {% if 'siteurl.com' not in child.url %}
       {% set linkTarget = 'target="_blank"' %}
  {% end if %}
  
  <a href="{{child.url}}" {{ linkTarget }}>{{child.title}}</a>
  
  ```
  
  Note you can sub the `{% if 'siteurl.com' not in child.url %}` for `{% if child.fieldName == true %}`
  
  ---
  
  **Closing Note**
  
  It would be most helpful if you could explain how you have created the navigation in the admin. I am doing a bit of guesswork at the moment. Please let me know if these options work, or if you have more information for me to review.
  
  
  ---
  
  