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:


Section: Structure


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 %}
| improve this answer | |
  • Brilliant Krystof, where can I find the excludeEntry tag? Is this Craft or Twig language? – Nutmeg Nov 15 '16 at 20:55
  • excludeEntry is just a twig variable (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 Nov 16 '16 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.