<p>The entries field will give you its value in the form of an <a href="https://craftcms.com/docs/4.x/entries.html#querying-entries" rel="nofollow noreferrer"><code>EntryQuery</code></a>, which you can execute to get the actual entry. I'm guessing your <code>navUrl</code> field allows only one entry? In this case, it's best to execute the query with <code>.one()</code>, which will return either the entry selected in the field or <code>null</code> if no entry is selected.</p>
<pre class="lang-twig prettyprint-override"><code>{% set navEntries = craft.entries().section('mainMenu').all() %}

{% for entry in navEntries %}
    {% set linkedEntry = entry.navUrl.one() %}
    {% set targetUrl = linkedEntry ? linkedEntry.getUrl() : entry.customUrl %}
{% endfor %}
</code></pre>
<p>You can reduce that to a handy one-liner with the null coalescing operator:</p>
<pre class="lang-twig prettyprint-override"><code>{% set navEntries = craft.entries().section('mainMenu').all() %}

{% for entry in navEntries %}
    {% set targetUrl = entry.navUrl.one().getUrl() ?? entry.customUrl %}
%}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2022-08-17T11:49:19.673Z
2022-08-17T11:49:19.673Z
MoritzLost
<p>I am new to craftcms and searching did not get any answer. I have 2 fields setup navUrl (type entries) and customUrl (url) and I have a section called mainMenu with these fields. I have then added the entries for this section with each navUrl pointing to the structure entries (home, about etc).</p>
<p>For example my site has</p>
<p>Home
Blog
about
contact</p>
<p>In template I am can access the title, customUrl but unable to access navUrl which has the url for home page, about page etc.</p>
<p>How do I access the url which is selected in navUrl? doing a dump actually shows the field navUrl as null!</p>
<p><a href="https://craftcms.stackexchange.com/questions/17898/getting-the-uri-of-an-entries-field-in-a-structure-with-no-urls">Getting the URI of an Entries field in a Structure with no URLs</a> - solution mentioned doesnt work</p>

<structure><navigation><entries-field><craft4>
Navigation from structure with field Entries for URL
# Navigation from structure with field Entries for URL

(Asked by: username_0 on 2022-08-17)

I am new to craftcms and searching did not get any answer. I have 2 fields setup navUrl (type entries) and customUrl (url) and I have a section called mainMenu with these fields. I have then added the entries for this section with each navUrl pointing to the structure entries (home, about etc).

For example my site has

Home
Blog
about
contact

In template I am can access the title, customUrl but unable to access navUrl which has the url for home page, about page etc.

How do I access the url which is selected in navUrl? doing a dump actually shows the field navUrl as null!

[Getting the URI of an Entries field in a Structure with no URLs](https://craftcms.stackexchange.com/questions/17898/getting-the-uri-of-an-entries-field-in-a-structure-with-no-urls) - solution mentioned doesnt work

**Tags:** structure, navigation, entries-field, craft4


---

## 1 answers

### Answer by username_1 on 2022-08-17. Score: 1

The entries field will give you its value in the form of an EntryQuery, which you can execute to get the actual entry. I'm guessing your navUrl field allows only one entry? In this case, it's best to execute the query with .one(), which will return either the entry selected in the field or null if no entry is selected.
{% set navEntries = craft.entries().section('mainMenu').all() %}

{% for entry in navEntries %}
    {% set linkedEntry = entry.navUrl.one() %}
    {% set targetUrl = linkedEntry ? linkedEntry.getUrl() : entry.customUrl %}
{% endfor %}

You can reduce that to a handy one-liner with the null coalescing operator:
{% set navEntries = craft.entries().section('mainMenu').all() %}

{% for entry in navEntries %}
    {% set targetUrl = entry.navUrl.one().getUrl() ?? entry.customUrl %}
%}
{% endfor %}


---

