Answers.Body: <p>In short, don't use the <code>{% nav %}</code> tag as it's not flexible enough for what you need.</p>
  <p>Assuming your fields are called <code>entriesFieldURL</code> and <code>categoriesFieldURL</code>, try something like this:</p>
  <pre class="lang-twig prettyprint-override"><code>{# Fetch the current page ID to use for your active class later on. If entry exists, we are on an entry page, else, category page or NULL fallback #}
  {% set currentPageId = entry.id ?? category.id ?? null %}
  
  {# Fetch your entries and eager-load children #}
  {% set menu = craft.entries()
      .section('navDropDownMenu')
      .level(1)
      .with('children')
      .all() %}
  
  &lt;ul&gt;
  {%- for item in menu -%}
      
      {# Set a link object which switches based on whether you have a category or an entry #}
      {%- set link = item.entriesFieldURL|length ? item.entriesFieldURL[0] : item.categoriesFieldURL.leaves()[0] -%}
      &lt;li&gt;
          
          {# Output the link and add active class base on the current page when needed #}
          &lt;a href=&quot;{{ link.url }}&quot; class=&quot;{{ link.id == currentPageId ? 'active' }}&quot;&gt;{{ link.title }}&lt;/a&gt;
          
          {# Does this item have children? #}
          {%- if item.children -%}
          &lt;ul&gt;
              {%- for item in item.children -%}
              
              {# As above, set a link object which switches based on whether you have a category or an entry #}
              {%- set link = item.entriesFieldURL|length ? item.entriesFieldURL[0] : item.categoriesFieldURL.leaves()[0] -%}
              &lt;li&gt;
                  
                  {# And again, output the link and add active class when needed #}
                  &lt;a href=&quot;{{ link.url }}&quot; class=&quot;{{ link.id == currentPageId ? 'active' }}&quot;&gt;{{ link.title }}&lt;/a&gt;
              &lt;/li&gt;
              {%- endfor -%}
          &lt;/ul&gt;
          {%- endif -%}
      &lt;/li&gt;
  {%- endfor -%}
  &lt;/ul&gt;
  </code></pre>
  <p>As you can see, there's some repetition in there which you could solve by using <a href="https://craftcms.com/docs/3.x/dev/twig-primer.html#macros" rel="nofollow noreferrer">a short macro</a> to build the <code>&lt;a&gt;</code> tags making the complete thing:</p>
  <pre class="lang-twig prettyprint-override"><code>{# Fetch the current page ID to use for your active class later on. If entry exists, we are on an entry page, else, category page or NULL fallback #}
  {% set currentPageId = entry.id ?? category.id ?? null %}
  
  {# Fetch your entries and eager-load children #}
  {% set menu = craft.entries()
      .section('navDropDownMenu')
      .level(1)
      .with('children')
      .all() %}
  
  {% macro linkItem(item, currentPageId) %}
      {%- set link = item.entriesFieldURL|length ? item.entriesFieldURL[0] : item.categoriesFieldURL.leaves()[0] -%}
      &lt;a href=&quot;{{ link.url }}&quot; class=&quot;{{ link.id == currentPageId ? 'active' }}&quot;&gt;{{ link.title }}&lt;/a&gt;
  {% endmacro %}
  
  &lt;ul&gt;
  {%- for item in menu -%}
      &lt;li&gt;
          {{ _self.linkItem(item, currentPageId) }}
          {%- if item.children -%}
          &lt;ul&gt;
              {%- for item in item.children -%}
                  &lt;li&gt;
                      {{ _self.linkItem(item, currentPageId) }}
                  &lt;/li&gt;
              {%- endfor -%}
          &lt;/ul&gt;
          {%- endif -%}
      &lt;/li&gt;
  {%- endfor -%}
  &lt;/ul&gt;
  </code></pre>
  <p>You could further refine but hopefully, this gets you started :)</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-01T18:25:37.903Z
Answers.Text: This has the same problem, it can't handle sub-categories (it will only link to the top level).
Answers.UserDisplayName: doctorgloom
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-02T10:33:56.657Z
Answers.Text: Do you mean if you choose a subcategory in your categoriesFieldURL?
Answers.UserDisplayName: Oli
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-02T19:12:21.473Z
Answers.Text: No, what I'm saying is, if I have a Category Field, and the category I choose is a subcategory (like my example Shoe > Tennis) Then what appears is only the Parent category, which I don't want in that case. Put another way, I'm accessing product pages by category, so what I need from a Navigation element is to link to the Parent as well as Sub categories, depending on the nav item. My idea with this Structure was that I could have some Nav items that are Entries and Some that are Categories (parent and sub), but only the Parent outputs.
Answers.UserDisplayName: doctorgloom
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-02T20:43:43.787Z
Answers.Text: I just re-read your question and I think I misunderstood. Yes, if the choice is a sub-category, it ignores that choice and outputs the Parent.
Answers.UserDisplayName: doctorgloom
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-03T07:24:45.223Z
Answers.Text: I changed the code in the answer, could you give that another go?
Answers.UserDisplayName: Oli
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-03T19:04:18.080Z
Answers.Text: I spoke too soon. That works fine if it is a sub-category, but if the link is a Parent category it outputs nothing. Basically the idea is, I want to create a structure for the nav, some of the nav links will be a Parent Category, some of them will be a Sub Category, and some will be a link to an Entry.
Answers.UserDisplayName: doctorgloom
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-04T07:40:20.280Z
Answers.Text: I just tried to test this and it works for me. Are you using the macro example and if so, how deep are the categories?
Answers.UserDisplayName: Oli
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-04T20:05:11.060Z
Answers.Text: Yes, copied and pasted your Macro code. Not sure what I'm doing wrong, but if link is top level category it outputs an empty anchor element. I'll dig a little more and see if I missed something, but I might just kill the idea. But thanks for the help with it.
Answers.UserDisplayName: doctorgloom
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-01-30T10:38:53.540Z
Answers.LastActivityDate: 2021-02-04T07:34:56.143Z
Answers.LastEditDate: 2021-02-04T07:34:56.143Z
Answers.UserDisplayName: Oli
Body: <p>I am creating a <em>Structure</em> for the main navigation that has two possible choices for the Link: <strong>Entries field</strong> or a <strong>Categories field</strong>. The answer I received here showed me <a href="https://craftcms.stackexchange.com/questions/37086/structure-navigation-with-3-possible-link-types-using-nav-tag">how to setup the basics</a> but if the Category page I want to link to is a Sub Category, it will only link to the Parent.</p>
  <pre><code>{% set menu = craft.entries.section('navDropDownMenu').all() %}
  &lt;ul&gt;
      {% nav link in menu %}
          &lt;li&gt;
          {% if link.entriesURL|length %}
              {% if craft.app.request.absoluteUrl ==  link.entriesFieldURL[0].url %}
                  &lt;a href=&quot;{{ link.entriesFieldURL[0].url }}&quot; class=&quot;active&quot;&gt;{{ link.title }}&lt;/a&gt;
              {% else %}
                  &lt;a href=&quot;{{ link.entriesFieldURL[0].url }}&quot;&gt;{{ link.title }}&lt;/a&gt;
              {% endif %}
          {% else %}
              {% if craft.app.request.absoluteUrl ==  link.categoryFieldURL[0].url %}
                  &lt;a href=&quot;{{ link.categoryFieldURL[0].url }}&quot; class=&quot;active&quot;&gt;{{ link.title }}&lt;/a&gt;
              {% else %}
                  &lt;a href=&quot;{{ link.categoryFieldURL[0].url }}&quot;&gt;{{ link.title }}&lt;/a&gt;
              {% endif %}
          {% endif %} 
              {% ifchildren %}
                  &lt;ul&gt;
                      {% children %}
                  &lt;/ul&gt;
              {% endifchildren %}
          &lt;/li&gt;
      {% endnav %}
  </code></pre>
  <p>So what I'm trying to figure out is, if I have a category setup like this:</p>
  <pre><code>-Shoes
      -Tennis
      
  </code></pre>
  <p>in the context of using the Nav Tag (as above), how do I get the link to 'Tennis' and not 'Shoes'?</p>
  <p>I know you can output a nav of Categories, but I wanted to use a Structure if possible because the navigation has a mixture of categories and links to internal pages, and they are mixed together in the navigation as far as sorting goes.</p>
  
Tags: <categories><navigation>
Title: Get the Sub-Category Link from Category field in a Structure (output with Nav tag)
ThreadText: # Get the Sub-Category Link from Category field in a Structure (output with Nav tag)
  
  (Asked by: username_0 on 2021-01-29)
  
  I am creating a *Structure* for the main navigation that has two possible choices for the Link: **Entries field** or a **Categories field**. The answer I received here showed me [how to setup the basics](https://craftcms.stackexchange.com/questions/37086/structure-navigation-with-3-possible-link-types-using-nav-tag) but if the Category page I want to link to is a Sub Category, it will only link to the Parent.
  
  ```
  {% set menu = craft.entries.section('navDropDownMenu').all() %}
  <ul>
      {% nav link in menu %}
          <li>
          {% if link.entriesURL|length %}
              {% if craft.app.request.absoluteUrl ==  link.entriesFieldURL[0].url %}
                  <a href="{{ link.entriesFieldURL[0].url }}" class="active">{{ link.title }}</a>
              {% else %}
                  <a href="{{ link.entriesFieldURL[0].url }}">{{ link.title }}</a>
              {% endif %}
          {% else %}
              {% if craft.app.request.absoluteUrl ==  link.categoryFieldURL[0].url %}
                  <a href="{{ link.categoryFieldURL[0].url }}" class="active">{{ link.title }}</a>
              {% else %}
                  <a href="{{ link.categoryFieldURL[0].url }}">{{ link.title }}</a>
              {% endif %}
          {% endif %} 
              {% ifchildren %}
                  <ul>
                      {% children %}
                  </ul>
              {% endifchildren %}
          </li>
      {% endnav %}
  
  ```
  
  So what I'm trying to figure out is, if I have a category setup like this:
  
  ```
  -Shoes
      -Tennis
      
  
  ```
  
  in the context of using the Nav Tag (as above), how do I get the link to 'Tennis' and not 'Shoes'?
  
  I know you can output a nav of Categories, but I wanted to use a Structure if possible because the navigation has a mixture of categories and links to internal pages, and they are mixed together in the navigation as far as sorting goes.
  
  **Tags:** categories, navigation
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-01-30. Score: 2
  
  In short, don't use the `{% nav %}` tag as it's not flexible enough for what you need.
  
  Assuming your fields are called `entriesFieldURL` and `categoriesFieldURL`, try something like this:
  
  ```twig
  {# Fetch the current page ID to use for your active class later on. If entry exists, we are on an entry page, else, category page or NULL fallback #}
  {% set currentPageId = entry.id ?? category.id ?? null %}
  
  {# Fetch your entries and eager-load children #}
  {% set menu = craft.entries()
      .section('navDropDownMenu')
      .level(1)
      .with('children')
      .all() %}
  
  <ul>
  {%- for item in menu -%}
      
      {# Set a link object which switches based on whether you have a category or an entry #}
      {%- set link = item.entriesFieldURL|length ? item.entriesFieldURL[0] : item.categoriesFieldURL.leaves()[0] -%}
      <li>
          
          {# Output the link and add active class base on the current page when needed #}
          <a href="{{ link.url }}" class="{{ link.id == currentPageId ? 'active' }}">{{ link.title }}</a>
          
          {# Does this item have children? #}
          {%- if item.children -%}
          <ul>
              {%- for item in item.children -%}
              
              {# As above, set a link object which switches based on whether you have a category or an entry #}
              {%- set link = item.entriesFieldURL|length ? item.entriesFieldURL[0] : item.categoriesFieldURL.leaves()[0] -%}
              <li>
                  
                  {# And again, output the link and add active class when needed #}
                  <a href="{{ link.url }}" class="{{ link.id == currentPageId ? 'active' }}">{{ link.title }}</a>
              </li>
              {%- endfor -%}
          </ul>
          {%- endif -%}
      </li>
  {%- endfor -%}
  </ul>
  
  ```
  
  As you can see, there's some repetition in there which you could solve by using [a short macro](https://craftcms.com/docs/3.x/dev/twig-primer.html#macros) to build the `<a>` tags making the complete thing:
  
  ```twig
  {# Fetch the current page ID to use for your active class later on. If entry exists, we are on an entry page, else, category page or NULL fallback #}
  {% set currentPageId = entry.id ?? category.id ?? null %}
  
  {# Fetch your entries and eager-load children #}
  {% set menu = craft.entries()
      .section('navDropDownMenu')
      .level(1)
      .with('children')
      .all() %}
  
  {% macro linkItem(item, currentPageId) %}
      {%- set link = item.entriesFieldURL|length ? item.entriesFieldURL[0] : item.categoriesFieldURL.leaves()[0] -%}
      <a href="{{ link.url }}" class="{{ link.id == currentPageId ? 'active' }}">{{ link.title }}</a>
  {% endmacro %}
  
  <ul>
  {%- for item in menu -%}
      <li>
          {{ _self.linkItem(item, currentPageId) }}
          {%- if item.children -%}
          <ul>
              {%- for item in item.children -%}
                  <li>
                      {{ _self.linkItem(item, currentPageId) }}
                  </li>
              {%- endfor -%}
          </ul>
          {%- endif -%}
      </li>
  {%- endfor -%}
  </ul>
  
  ```
  
  You could further refine but hopefully, this gets you started :)
  
  > **Comments:**
  > **username_0**: This has the same problem, it can't handle sub-categories (it will only link to the top level).  
  > **username_1**: Do you mean if you choose a subcategory in your categoriesFieldURL?  
  > **username_0**: No, what I'm saying is, if I have a Category Field, and the category I choose is a subcategory (like my example Shoe > Tennis) Then what appears is only the Parent category, which I don't want in that case. Put another way, I'm accessing product pages by category, so what I need from a Navigation element is to link to the Parent as well as Sub categories, depending on the nav item. My idea with this Structure was that I could have some Nav items that are Entries and Some that are Categories (parent and sub), but only the Parent outputs.  
  > **username_0**: I just re-read your question and I think I misunderstood. Yes, if the choice is a sub-category, it ignores that choice and outputs the Parent.  
  > **username_1**: I changed the code in the answer, could you give that another go?  
  > **username_0**: I spoke too soon. That works fine if it is a sub-category, but if the link is a Parent category it outputs nothing. Basically the idea is, I want to create a structure for the nav, some of the nav links will be a Parent Category, some of them will be a Sub Category, and some will be a link to an Entry.  
  > **username_1**: I just tried to test this and it works for me. Are you using the macro example and if so, how deep are the categories?  
  > **username_0**: Yes, copied and pasted your Macro code. Not sure what I'm doing wrong, but if link is top level category it outputs an empty anchor element. I'll dig a little more and see if I missed something, but I might just kill the idea. But thanks for the help with it.  
  
  ---
  
  