<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>

CC BY-SA 4.0
2024-01-11T22:41:12.200Z
2024-01-11T22:41:12.200Z
kelly
<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>

<craft4>
how to add an if to open an external link in the menu
# 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 %}

    
        {% for menuItem in menu %} 
            
                {% set links = menuItem.linkDeDestino.all() %}
                    {% if links|length > 1 %}     
                        {{menuItem.tituloDaOpcao}} 
                        
                            {% for child in links %} 
                                
                                    {{child.title}}

                                    {% if child.hasDescendants %}  

                                    {% set descendants = craft.entries()  
                                        .descendantOf(child)
                                        .all() %} 
                                    
                                        {% for child in descendants %} 
                                        
                                            {{child.title}}
                                        
                                        {% endfor %}
                                    
                                    {% endif %}
                                
                            {% endfor %}
                        
                    {% else %}  

                    {% set link =  menuItem.linkDeDestino.one() %} 
                        {{menuItem.tituloDaOpcao}}
                    {% if link.hasDescendants %}
                        {% set descendants = craft.entries()
                                        .descendantOf(link)
                                        .all() %}
                        
                            {% for child in descendants %}
                                {{child.title}}
                            {% endfor %}
                        
                    {% endif %}

                {% endif %}

                
            
        {% endfor %}
    

{% 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:

**Tags:** craft4


---

## 1 answers

### Answer by username_1 on 2024-01-11. Score: 1

Using Navigation Plugin
Are you using the "Navigation" 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.



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:
{{child.title}}

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 %}

{{child.title}}

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.


---

