Answers.Body: <p>Instead of using <em>nav</em>, you can just use your own code for getting children. I.e. loop through level 1, then within that loop check if they have ancestors at level 2, then within that loop check if they have ancestors for level 3 etc.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-02T15:26:26.463Z
Answers.LastActivityDate: 2019-09-02T15:26:26.463Z
Answers.UserDisplayName: dmatthams
Body: <p>The following is using commerce product entries, but I'm reasonably sure it could be any entry.</p>
  
  <p>With the following category taxonomy:</p>
  
  <pre><code>- Category 1
      - Category 1A
  - Category 2
  - Category 3
  ...
  </code></pre>
  
  <p>If I have a product entry "Product 1" with a category field selected to "Category 1A", when looping though 
  all categories to return products for that category, how can I list the entry ONLY for "Category 1A" and not it's any of its ancestors e.g. "Category 1"?</p>
  
  <p>Currently, I'm using the following:</p>
  
  <pre><code>{% nav category in productCategories %}
      &lt;li&gt;
          {{ category.title }}
          {% set products = craft.products().relatedTo(category).all() %}
          {% for product in products %}
              {{ product.title }}
          {% endfor %}
          {% ifchildren %}
              &lt;ul&gt;
                  {% children %}
              &lt;/ul&gt;
          {% endifchildren %}
      &lt;/li&gt;
  {% endnav %}
  </code></pre>
  
  <p>Which returns</p>
  
  <pre><code>- Category 1
      Product 1
      - Category 1A
          Product 1
  - Category 2
  - Category 3
  ...
  </code></pre>
  
  <p>But what I want is for it to only be visible on the selected category.</p>
  
  <pre><code>- Category 1
      - Category 1A
          Product 1
  - Category 2
  - Category 3
  ...
  </code></pre>
  
  <p>Is there a way to do this using categories? or am I best using a structure?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-09-02T11:22:55.003Z
Comments.Text: Using a structure seems to work however I would like to retain the ability of getting all entries for a parent category, best of both worlds!?
Comments.UserDisplayName: Matt Jenkins
Tags: <templating><categories><entry><relatedto>
Title: Get entries related to selected category only and not it's ancestors
ThreadText: # Get entries related to selected category only and not it's ancestors
  
  (Asked by: username_0 on 2019-08-30)
  
  The following is using commerce product entries, but I'm reasonably sure it could be any entry.
  
  With the following category taxonomy:
  
  ```
  - Category 1
      - Category 1A
  - Category 2
  - Category 3
  ...
  
  ```
  
  If I have a product entry "Product 1" with a category field selected to "Category 1A", when looping though
  all categories to return products for that category, how can I list the entry ONLY for "Category 1A" and not it's any of its ancestors e.g. "Category 1"?
  
  Currently, I'm using the following:
  
  ```
  {% nav category in productCategories %}
      <li>
          {{ category.title }}
          {% set products = craft.products().relatedTo(category).all() %}
          {% for product in products %}
              {{ product.title }}
          {% endfor %}
          {% ifchildren %}
              <ul>
                  {% children %}
              </ul>
          {% endifchildren %}
      </li>
  {% endnav %}
  
  ```
  
  Which returns
  
  ```
  - Category 1
      Product 1
      - Category 1A
          Product 1
  - Category 2
  - Category 3
  ...
  
  ```
  
  But what I want is for it to only be visible on the selected category.
  
  ```
  - Category 1
      - Category 1A
          Product 1
  - Category 2
  - Category 3
  ...
  
  ```
  
  Is there a way to do this using categories? or am I best using a structure?
  
  **Tags:** templating, categories, entry, relatedto
  
  > **Comments:**
  > **username_0**: Using a structure seems to work however I would like to retain the ability of getting all entries for a parent category, best of both worlds!?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-09-02. Score: 1
  
  Instead of using *nav*, you can just use your own code for getting children. I.e. loop through level 1, then within that loop check if they have ancestors at level 2, then within that loop check if they have ancestors for level 3 etc.
  
  
  ---
  
  