5

How can I get the top level ancestor of an entry within a structure? I've tried:

{% set rootEntry = null %}
{% for ancestor in entry.getAncestors() %}
    {% set rootEntry = ancestor %}
{% endfor %}

But that only returns the most recent ancestor back and doesn't go all the way up the chain.

Jared Meyering
  • 211
  • 1
  • 4

1 Answers1

7

getAncestors() gets all entries that are ancestors. Since you're looping through them what rootEntry is set to last time through is the closest ancestor.

You can avoid the loop and just get the first one:

{% set rootEntry = entry.getAncestors().first() %}
Simon Kuran
  • 3,015
  • 1
  • 18
  • 33