<p>Found the solution, thanks to Aaron Berkowitz on <a href="https://craftcms.stackexchange.com/questions/11037/how-to-get-entries-related-to-current-category-only-not-children?rq=1">How to get entries related to current category only (not children)</a></p>

<p>You need to exclude the entries related to the subcategories.</p>

<pre><code> {% if category is defined %}
 {% set dontInclude = [] %}
     {% for child in craft.categories.descendantOf(category) %}
        {% set dontInclude = dontInclude|merge(craft.entries.relatedTo(child).ids) %}
      {% endfor %}

      {% set str = dontInclude|join(', not ') %}


  {% if dontInclude|length &gt; 0 %}
    {% set params = params | merge({
      relatedTo: category,
      order: 'order ASC',
      id: 'and, not '~str
    }) %}
  {% else %}
    {% set params = params | merge({
      relatedTo: category,
      order: 'order ASC'
    }) %}
  {% endif %}
    {% endif %}

    &lt;!-- Get all entries by params --&gt;
    {% set blogEntries = craft.entries(params) %}

    &lt;!-- Paginate entries (9 per page) --&gt;
    {% paginate blogEntries.limit(2) as pageInfo, pageEntries %}
&lt;ul&gt;
      {% for entry in pageEntries %}
        &lt;li&gt;{{ entry.title }} ({{ entry.productCategory|join(', ') }})&lt;/li&gt;
      {% endfor %}
    &lt;/ul&gt;
</code></pre>

CC BY-SA 3.0
2017-11-14T02:43:11.140Z
2017-11-14T03:54:10.250Z
2017-11-14T03:54:10.250Z
Teegan Lincoln
<p>I want to only display entries on the category page which is the last in the array of the entries category, so that it doesn't also show on parent category page.</p>

<p>Let say these are my categories:</p>

<p>Shop</p>

<ul>
<li>List item</li>
<li>Shoes</li>
<li>T-shirts</li>
</ul>

<p>Entries with a category of "shoes" displays under both "shop" and "shoes" because of the way categories work as an array. My aim is to have it show only on "shoes", not "shop".</p>

<p>Here is my current code:</p>

<pre><code>&lt;!-- Get entries from the shop section --&gt;
    {% set params = {
      section: 'shop'
    } %}

    &lt;!-- Check if curreny entry is a category page, Set params to get entries related to current category, order by entry "order" field --&gt;
    {% if category is defined %}
      {% set params = params | merge({
        relatedTo: category
      }) %}
    {% endif %}

    &lt;!-- Get all entries by params --&gt;
    {% set blogEntries = craft.entries(params) %}

    &lt;!-- Paginate entries (9 per page) --&gt;
    {% paginate blogEntries.limit(9) as pageInfo, pageEntries %}

    &lt;!-- Display each entry, only if the current category is the same as the LAST category in the entries array. The conditional makes sure that the entries with non-top-level categories aren't displayed under parent categories. --&gt;
    &lt;ul&gt;
      {# for entry in pageEntries if category.id == entry.productCategory.last().id #}
      {% for entry in pageEntries %}
        &lt;li&gt;{{ entry.title }} ({{ entry.productCategory | join(', ') }})&lt;/li&gt;
      {% endfor %}
    &lt;/ul&gt;
  &lt;/div&gt;

  {% if blogEntries|length &gt; 9 %}
    {% include "_partials/pagination.twig" %}
  {% endif %}
</code></pre>

<p>I've managed to display the entries under the correct categories with the 
<code>if category.id == entry.productCategory.last().id</code> on the for loop displaying all the entries, however this does not work with pagination. </p>

<p>My question is, is there a way to bake the conditional into the initial category params to avoid looping through all entries where the current category doesn't match the last category within the entry's array. </p>

<p>I hope this makes sense, please feel free to get in touch for any clarification.</p>

<p>Thanks,
Teegan</p>

<entries><pagination><category><craft.entries>
Displaying entries with pagination on a category page where the entry's last category in the array is the current category
# Displaying entries with pagination on a category page where the entry's last category in the array is the current category

(Asked by: username_0 on 2017-11-14)

I want to only display entries on the category page which is the last in the array of the entries category, so that it doesn't also show on parent category page.
Let say these are my categories:
Shop

List item
Shoes
T-shirts

Entries with a category of "shoes" displays under both "shop" and "shoes" because of the way categories work as an array. My aim is to have it show only on "shoes", not "shop".
Here is my current code:

    {% set params = {
      section: 'shop'
    } %}

    
    {% if category is defined %}
      {% set params = params | merge({
        relatedTo: category
      }) %}
    {% endif %}

    
    {% set blogEntries = craft.entries(params) %}

    
    {% paginate blogEntries.limit(9) as pageInfo, pageEntries %}

    
    
      {# for entry in pageEntries if category.id == entry.productCategory.last().id #}
      {% for entry in pageEntries %}
        {{ entry.title }} ({{ entry.productCategory | join(', ') }})
      {% endfor %}
    
  

  {% if blogEntries|length > 9 %}
    {% include "_partials/pagination.twig" %}
  {% endif %}

I've managed to display the entries under the correct categories with the 
if category.id == entry.productCategory.last().id on the for loop displaying all the entries, however this does not work with pagination. 
My question is, is there a way to bake the conditional into the initial category params to avoid looping through all entries where the current category doesn't match the last category within the entry's array. 
I hope this makes sense, please feel free to get in touch for any clarification.
Thanks,
Teegan

**Tags:** entries, pagination, category, craft.entries


---

## 1 answers

### Answer by username_0 on 2017-11-14. Score: 1 | Accepted answer

Found the solution, thanks to Aaron Berkowitz on How to get entries related to current category only (not children)
You need to exclude the entries related to the subcategories.
 {% if category is defined %}
 {% set dontInclude = [] %}
     {% for child in craft.categories.descendantOf(category) %}
        {% set dontInclude = dontInclude|merge(craft.entries.relatedTo(child).ids) %}
      {% endfor %}

      {% set str = dontInclude|join(', not ') %}


  {% if dontInclude|length > 0 %}
    {% set params = params | merge({
      relatedTo: category,
      order: 'order ASC',
      id: 'and, not '~str
    }) %}
  {% else %}
    {% set params = params | merge({
      relatedTo: category,
      order: 'order ASC'
    }) %}
  {% endif %}
    {% endif %}

    
    {% set blogEntries = craft.entries(params) %}

    
    {% paginate blogEntries.limit(2) as pageInfo, pageEntries %}

      {% for entry in pageEntries %}
        {{ entry.title }} ({{ entry.productCategory|join(', ') }})
      {% endfor %}


---

