<p>Get the first category of the entry you're currently on :</p>

<pre><code>{% set currentEntryCategory = entry.categoryHandle.one().id %}
</code></pre>

<p>This assumes you limited the category to only be assigned once to the entry, otherwise you'll have to loop through all assigned categories to find the nested one you want.</p>

<p>Use that category ID to get the other entries that are relatedTo it :</p>

<pre><code>{% set entries = craft.entries.relatedTo(currentEntryCategory).all() %}
{% for relatedEntry in entries %}
    {{ relatedEntry.title }}
{% endfor %}
</code></pre>

<p>If you want to exclude the current entry that you're on from the query :</p>

<pre><code>{% set entries = craft.entries.relatedTo(currentEntryCategory).id('not ' ~ entry.id).all() %}
</code></pre>

CC BY-SA 4.0
2020-02-15T01:07:27.140Z
2020-02-15T01:07:27.140Z
Kelly B
<p>Thanks for the code snippets, this now works (below) as I was using .last with craft 3 and not .inReverse().one(),</p>

<pre><code>{% set currentEntryCategory = entry.productCategory.one() %}
{% set entry = craft.entries.relatedTo(currentEntryCategory).inReverse().one() %}

&lt;p&gt;{{ entry.title }}&lt;/p&gt;
</code></pre>

CC BY-SA 4.0
2020-02-17T11:32:25.437Z
2020-02-17T11:32:25.437Z
user2569
<p>I'm trying to show the other entries assigned to the same nested category (on an entry template)
but get an error if I use <code>.last()</code> which I was hoping would show the nested category assigned entries (and not all assigned to all levels of the same category,</p>

<p>Any ideas?</p>

<pre><code>{# set the category group #}
{% set categoryGroup = craft.categories.group('products') %}

{% set entries = craft.entries.relatedTo(categoryGroup)  %}  

{# only show entries assigned to the same nested category #}
{% for sameCat in entries.last()  %}  
    &lt;p&gt;{{ sameCat.title }}&lt;/p&gt;
{% endfor %}

{# note: using .last throws error – Impossible to access an attribute ("title") on a string variable ("1").
 #}
</code></pre>

<categories><relatedto><nested>
Display entries sharing the same nested category on an entry page
# Display entries sharing the same nested category on an entry page

(Asked by: username_0 on 2020-02-14)

I'm trying to show the other entries assigned to the same nested category (on an entry template)
but get an error if I use .last() which I was hoping would show the nested category assigned entries (and not all assigned to all levels of the same category,
Any ideas?
{# set the category group #}
{% set categoryGroup = craft.categories.group('products') %}

{% set entries = craft.entries.relatedTo(categoryGroup)  %}  

{# only show entries assigned to the same nested category #}
{% for sameCat in entries.last()  %}  
    {{ sameCat.title }}
{% endfor %}

{# note: using .last throws error – Impossible to access an attribute ("title") on a string variable ("1").
 #}

**Tags:** categories, relatedto, nested


---

## 2 answers

### Answer by username_1 on 2020-02-15. Score: 1

Get the first category of the entry you're currently on :
{% set currentEntryCategory = entry.categoryHandle.one().id %}

This assumes you limited the category to only be assigned once to the entry, otherwise you'll have to loop through all assigned categories to find the nested one you want.
Use that category ID to get the other entries that are relatedTo it :
{% set entries = craft.entries.relatedTo(currentEntryCategory).all() %}
{% for relatedEntry in entries %}
    {{ relatedEntry.title }}
{% endfor %}

If you want to exclude the current entry that you're on from the query :
{% set entries = craft.entries.relatedTo(currentEntryCategory).id('not ' ~ entry.id).all() %}


---

### Answer by username_0 on 2020-02-17. Score: 1

Thanks for the code snippets, this now works (below) as I was using .last with craft 3 and not .inReverse().one(),
{% set currentEntryCategory = entry.productCategory.one() %}
{% set entry = craft.entries.relatedTo(currentEntryCategory).inReverse().one() %}

{{ entry.title }}


---

