<p>No matter how many levels there are in your structure <a href="https://craftcms.com/docs/templating/craft.entries#ancestorOf" rel="nofollow noreferrer">ancestorOf</a> will fetch level one entry. <a href="https://craftcms.com/docs/templating/entrymodel#parent" rel="nofollow noreferrer">entry.parent</a> could be used but covers only one level up.</p>

<pre><code>{% set excludeEntry = entry %}
{% if entry.level &gt; 1 %}
    {% set excludeEntry = craft.entries.ancestorOf(entry).level(1).first() %}
{% endif %}

{% for entry in craft.entries.section('Thunder').level(1).id('not '~excludeEntry.id).limit(3).order('RAND()') %}
...
{% endfor %}
</code></pre>

CC BY-SA 3.0
2016-11-15T20:55:38.197Z
Brilliant Krystof, where can I find the excludeEntry tag? Is this Craft or Twig language?
Nutmeg
CC BY-SA 3.0
2016-11-16T10:14:36.413Z
`excludeEntry` is just a twig variable (http://twig.sensiolabs.org/doc/tags/set.html). I assign `entry` variable (provided by Craft) and overwrite it when entry level is greater then 1. This way we avoid redundant query to DB when entry level is equal to 1.
Krzysztof Boduch
CC BY-SA 3.0
2016-11-15T09:22:43.827Z
2016-11-15T09:22:43.827Z
Krzysztof Boduch
<p>I am trying to exclude the current entry in a "Recommended Reading" section which works at level 1 with the ID tag, but I also need it to do the same at level 2 (children). </p>

<pre><code> `{% for entry in craft.entries.section('Thunder').level(1).id('not '~entry.id).limit(3).order('RAND()') %}`
</code></pre>

<p>The level 1 entry is the Article and the level 2's are the Chapters. The recommended reading section displays other Articles and should therefor exclude the level(1) article also when viewing the Chapters. </p>

<p>Should I use <code>craft.request.getSegment(2)</code> to exclude the current entry, but how do you write that conditional statement?</p>

<p>Url structures are:</p>

<pre><code>/site/article
/site/article/chapter
</code></pre>

<p>Section: <strong>Structure</strong> </p>

<structure><conditional><segments>
Exclude entry based on segment
# Exclude entry based on segment

(Asked by: username_0 on 2016-11-15)

I am trying to exclude the current entry in a "Recommended Reading" section which works at level 1 with the ID tag, but I also need it to do the same at level 2 (children). 
 `{% for entry in craft.entries.section('Thunder').level(1).id('not '~entry.id).limit(3).order('RAND()') %}`

The level 1 entry is the Article and the level 2's are the Chapters. The recommended reading section displays other Articles and should therefor exclude the level(1) article also when viewing the Chapters. 
Should I use craft.request.getSegment(2) to exclude the current entry, but how do you write that conditional statement?
Url structures are:
/site/article
/site/article/chapter

Section: Structure

**Tags:** structure, conditional, segments

---

## 1 answers

### Answer by username_1 on 2016-11-15. Score: 0 | Accepted answer

No matter how many levels there are in your structure ancestorOf will fetch level one entry. entry.parent could be used but covers only one level up.
{% set excludeEntry = entry %}
{% if entry.level > 1 %}
    {% set excludeEntry = craft.entries.ancestorOf(entry).level(1).first() %}
{% endif %}

{% for entry in craft.entries.section('Thunder').level(1).id('not '~excludeEntry.id).limit(3).order('RAND()') %}
...
{% endfor %}

> **Comments:**
> **username_0**: Brilliant Krystof, where can I find the excludeEntry tag? Is this Craft or Twig language?  
> **username_1**: `excludeEntry` is just a twig variable (http://twig.sensiolabs.org/doc/tags/set.html). I assign `entry` variable (provided by Craft) and overwrite it when entry level is greater then 1. This way we avoid redundant query to DB when entry level is equal to 1.  

---

