Answers.Body: <p>Hopefully, I understood the question properly.</p>
  
  <p>You could start by fetching your <code>subcategories</code> <strong>ids</strong> doing:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set subcategories = craft
      .categories
      .group('prodCat')
      .descendantOf(prodCat)
      .level(2)
      .relatedto(entry)
      .ids() %}
  </code></pre>
  
  <p>Then query your entries at once rather than looping on your <code>subcategories</code>:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set relatedEntries = craft
      .entries
      .section('products')
      .relatedTo({
          targetElement : ['or'] | merge(subcategories) 
      })
      .id('not '~entry.id)
      .all() %}
  </code></pre>
  
  <p>So that you only loop your <code>relatedEntries</code> once.</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% if relatedEntries | length %}
      &lt;div&gt;
          &lt;h4&gt;Recommended Product&lt;/h4&gt;
          &lt;ul&gt;
          {% for entry in relatedEntries %}
              &lt;li&gt;{{entry.title}}&lt;/li&gt;
          {% endfor %}
          &lt;/ul&gt;
      &lt;/div&gt;
  {% endif %}
  </code></pre>
  
  <p>Altogether giving:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set subcategories = craft
      .categories()
      .group('prodCat')
      .descendantOf(prodCat)
      .level(2)
      .relatedto(entry)
      .ids() %}
  
  {% if subcategories %}
      {% set relatedEntries = craft
      .entries()
      .section('products')
      .relatedTo({
          targetElement : ['or'] | merge(subcategories) 
      })
      .id('not '~entry.id)
      .all() %}
  
      {% if relatedEntries %}
          &lt;div&gt;
              &lt;h4&gt;Recommended Product&lt;/h4&gt;
              &lt;ul&gt;
                  {% for entry in relatedentry %}
                  &lt;li&gt;{{entry.title}}&lt;/li&gt;
                  {% endfor %}
              &lt;/ul&gt;
          &lt;/div&gt;
      {% endif %}
  
  {% endif %}
  </code></pre>
  
  <p>If you really need to loop the way you are already doing, you could do:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% if loop.index == 1 %}
  &lt;div&gt;
      &lt;h4&gt;Recommended Product&lt;/h4&gt;
      &lt;ul&gt;
  {% endif %}
          {% for entry in relatedentry %}
          &lt;li&gt;{{entry.title}}&lt;/li&gt;
          {% endfor %}
  {% if loop.last %}
      &lt;/ul&gt;
  &lt;/div&gt;
  {% endif %}
  </code></pre>
  
  <p>Which would only print your header on the first iteration and close the <code>ul</code> and <code>div</code>. It's not as nice but it should do the job!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-13T03:41:31.443Z
Answers.Text: Fetching subcategories ids doesn't work. It didn't show my entry. But the last part 'loop.index' working fine. Thanks.
Answers.UserDisplayName: Lee
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-10T14:06:04.907Z
Answers.LastActivityDate: 2020-04-10T14:12:36.137Z
Answers.LastEditDate: 2020-04-10T14:12:36.137Z
Answers.UserDisplayName: Oli
Answers.Body: <p>You can collect the id from the previous entry, then you can exclude id in the next query. 
  For example like this :</p>
  
  <pre><code> {% set subcategories = craft.categories.group('prodCat').descendantOf(prodCat).level(2).relatedto(entry) %}
   {% set listId = ['not', entry.id ] %} //get id from current page id.
          {% if subcategories | length %}
              {% for subcategory in subcategories %}
                  {% set relatedentry = craft.entries.section('products').relatedTo(subcategories).id(listId).all() %}
                  {% set relatedentry_count = relatedentry|length %}
                  {% if relatedentry_count &gt; 0 %}
                  &lt;div&gt;
                      &lt;h4&gt;Recommended Product&lt;/h4&gt;
                      &lt;ul&gt;
                          {% for entry in relatedentry %}
                          {% set listId = listId|merge([entry.id]) %} // append the id from current category to the array.
                          &lt;li&gt;{{entry.title}}&lt;/li&gt;
                          {% endfor %}
                      &lt;/ul&gt;
                  &lt;/div&gt;
                  {% endif %}
              {% endfor %}
          {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-13T03:34:14.727Z
Answers.Text: This doesn't solve my issue. The title 'Recommended Product' shows twice when there are multiple categories.
Answers.UserDisplayName: Lee
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-10T12:22:12.533Z
Answers.LastActivityDate: 2020-04-10T12:22:12.533Z
Answers.UserDisplayName: aodihis
Body: <p>I want to show the entries which have same category of that entry. The entry might have multiple categories, so I want to merge the result. If there is a result, then only the whole structure will be shown. The title 'Recommended Product' should only show one time. Currently, the issue is if there are three categories, the whole structure will show three times.</p>
  
  <pre><code>{% set subcategories = craft.categories.group('prodCat').descendantOf(prodCat).level(2).relatedto(entry) %}
      {% if subcategories | length %}
          {% for subcategory in subcategories %}
              {% set relatedentry = craft.entries.section('products').relatedTo(subcategories).id('not '~entry.id).all() %}
              {% set relatedentry_count = relatedentry|length %}
              {% if relatedentry_count &gt; 0 %}
              &lt;div&gt;
                  &lt;h4&gt;Recommended Product&lt;/h4&gt;
                  &lt;ul&gt;
                      {% for entry in relatedentry %}
                      &lt;li&gt;{{entry.title}}&lt;/li&gt;
                      {% endfor %}
                  &lt;/ul&gt;
              &lt;/div&gt;
              {% endif %}
          {% endfor %}
      {% endif %}
  </code></pre>
  
  <p>Update based on the answer:</p>
  
  <p>I manage to solve the issue after making some tweak of the answer given, just merge the subcategory. </p>
  
  <pre><code>{% set subcategory = craft.categories.group('prodCat').level(2).relatedto(entry) %}
  {% set subcategories = subcategory|merge(subcategory) %}
      {% if subcategories | length %}
          {% set relatedentry = craft.entries.section('products').relatedTo(subcategories).id('not '~entry.id).all() %}
          {% set relatedentry_count = relatedentry|length %}
          {% if relatedentry_count &gt; 0 %}
              &lt;div&gt;
                  &lt;h4&gt;Recommended Product&lt;/h4&gt;
                  &lt;ul&gt;
                      {% for entry in relatedentry %}
                      &lt;li&gt;{{entry.title}}&lt;/li&gt;
                      {% endfor %}
                  &lt;/ul&gt;
              &lt;/div&gt;
          {% endif %}
      {% endif %}
  </code></pre>
  
Tags: <category><relatedto><merge>
Title: Show only and merge the entries from multiple categories related to that entry
ThreadText: # Show only and merge the entries from multiple categories related to that entry
  
  (Asked by: username_0 on 2020-04-10)
  
  I want to show the entries which have same category of that entry. The entry might have multiple categories, so I want to merge the result. If there is a result, then only the whole structure will be shown. The title 'Recommended Product' should only show one time. Currently, the issue is if there are three categories, the whole structure will show three times.
  
  ```
  {% set subcategories = craft.categories.group('prodCat').descendantOf(prodCat).level(2).relatedto(entry) %}
      {% if subcategories | length %}
          {% for subcategory in subcategories %}
              {% set relatedentry = craft.entries.section('products').relatedTo(subcategories).id('not '~entry.id).all() %}
              {% set relatedentry_count = relatedentry|length %}
              {% if relatedentry_count > 0 %}
              <div>
                  <h4>Recommended Product</h4>
                  <ul>
                      {% for entry in relatedentry %}
                      <li>{{entry.title}}</li>
                      {% endfor %}
                  </ul>
              </div>
              {% endif %}
          {% endfor %}
      {% endif %}
  
  ```
  
  Update based on the answer:
  
  I manage to solve the issue after making some tweak of the answer given, just merge the subcategory.
  
  ```
  {% set subcategory = craft.categories.group('prodCat').level(2).relatedto(entry) %}
  {% set subcategories = subcategory|merge(subcategory) %}
      {% if subcategories | length %}
          {% set relatedentry = craft.entries.section('products').relatedTo(subcategories).id('not '~entry.id).all() %}
          {% set relatedentry_count = relatedentry|length %}
          {% if relatedentry_count > 0 %}
              <div>
                  <h4>Recommended Product</h4>
                  <ul>
                      {% for entry in relatedentry %}
                      <li>{{entry.title}}</li>
                      {% endfor %}
                  </ul>
              </div>
          {% endif %}
      {% endif %}
  
  ```
  
  **Tags:** category, relatedto, merge
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2020-04-10. Score: 1 | Accepted answer
  
  Hopefully, I understood the question properly.
  
  You could start by fetching your `subcategories` **ids** doing:
  
  ```twig
  {% set subcategories = craft
      .categories
      .group('prodCat')
      .descendantOf(prodCat)
      .level(2)
      .relatedto(entry)
      .ids() %}
  
  ```
  
  Then query your entries at once rather than looping on your `subcategories`:
  
  ```twig
  {% set relatedEntries = craft
      .entries
      .section('products')
      .relatedTo({
          targetElement : ['or'] | merge(subcategories) 
      })
      .id('not '~entry.id)
      .all() %}
  
  ```
  
  So that you only loop your `relatedEntries` once.
  
  ```twig
  {% if relatedEntries | length %}
      <div>
          <h4>Recommended Product</h4>
          <ul>
          {% for entry in relatedEntries %}
              <li>{{entry.title}}</li>
          {% endfor %}
          </ul>
      </div>
  {% endif %}
  
  ```
  
  Altogether giving:
  
  ```twig
  {% set subcategories = craft
      .categories()
      .group('prodCat')
      .descendantOf(prodCat)
      .level(2)
      .relatedto(entry)
      .ids() %}
  
  {% if subcategories %}
      {% set relatedEntries = craft
      .entries()
      .section('products')
      .relatedTo({
          targetElement : ['or'] | merge(subcategories) 
      })
      .id('not '~entry.id)
      .all() %}
  
      {% if relatedEntries %}
          <div>
              <h4>Recommended Product</h4>
              <ul>
                  {% for entry in relatedentry %}
                  <li>{{entry.title}}</li>
                  {% endfor %}
              </ul>
          </div>
      {% endif %}
  
  {% endif %}
  
  ```
  
  If you really need to loop the way you are already doing, you could do:
  
  ```twig
  {% if loop.index == 1 %}
  <div>
      <h4>Recommended Product</h4>
      <ul>
  {% endif %}
          {% for entry in relatedentry %}
          <li>{{entry.title}}</li>
          {% endfor %}
  {% if loop.last %}
      </ul>
  </div>
  {% endif %}
  
  ```
  
  Which would only print your header on the first iteration and close the `ul` and `div`. It's not as nice but it should do the job!
  
  > **Comments:**
  > **username_0**: Fetching subcategories ids doesn't work. It didn't show my entry. But the last part 'loop.index' working fine. Thanks.  
  
  ---
  
  ### Answer by username_2 on 2020-04-10. Score: 1
  
  You can collect the id from the previous entry, then you can exclude id in the next query.
  For example like this :
  
  ```
   {% set subcategories = craft.categories.group('prodCat').descendantOf(prodCat).level(2).relatedto(entry) %}
   {% set listId = ['not', entry.id ] %} //get id from current page id.
          {% if subcategories | length %}
              {% for subcategory in subcategories %}
                  {% set relatedentry = craft.entries.section('products').relatedTo(subcategories).id(listId).all() %}
                  {% set relatedentry_count = relatedentry|length %}
                  {% if relatedentry_count > 0 %}
                  <div>
                      <h4>Recommended Product</h4>
                      <ul>
                          {% for entry in relatedentry %}
                          {% set listId = listId|merge([entry.id]) %} // append the id from current category to the array.
                          <li>{{entry.title}}</li>
                          {% endfor %}
                      </ul>
                  </div>
                  {% endif %}
              {% endfor %}
          {% endif %}
  
  ```
  
  > **Comments:**
  > **username_0**: This doesn't solve my issue. The title 'Recommended Product' shows twice when there are multiple categories.  
  
  ---
  
  