<p><code>entry.ancestors</code> returns an ElementCriteriaModel (→ <a href="http://buildwithcraft.com/docs/templating/entrymodel#getAncestors" rel="nofollow"><code>getAncestors()</code></a> in the Craft docs) prepped to return the entry's ancestors.</p>

<p><code>entry.ancestors.level(3)</code> makes it return only entry ancestors which are at 3rd level of the structure.</p>

<p>As you well pointed out in the comments, an entry can only have one ancestor on a particular structure level. So you can now use the <code>first()</code> method to get this one entry. Keep in mind that if there's no ancestor at that level, the method won't return anything (<code>null</code>).</p>

<pre><code>{% set ancestorL3 = entry.ancestors.level(3).first() %}

{% if ancestorL3 %}
    {{ ancestorL3.title }}
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-02-04T17:34:31.620Z
I see… I was thinking .level(3) would return a single entry as .first does (not sure how an entry could have more than one ancestor at a particular level). Thanks!
philzelnar
CC BY-SA 3.0
2015-02-04T18:12:36.173Z
@alsophil male and female?? ...errrm no.
carlcs
CC BY-SA 3.0
2015-02-04T15:42:48.960Z
2015-02-04T18:11:17.957Z
2015-02-04T18:11:17.957Z
carlcs
<p>I can grab a top/bottom level ancestor with something like entry.ancestors.first/last.title.</p>

<p>But, say I'm at level 6, is it possible to grab an ancestor at a particular level, like entry.ancestors.level(3).title? Or would you have to grab all the ancestors and loop through them to accomplish this?</p>

<p>New to deep structures and just want to confirm I'm not missing something that would allow me to sidestep a loop. Thanks!</p>

<structure>
Get ancestor at a specific level?
# Get ancestor at a specific level?

(Asked by: username_0 on 2015-02-04)

I can grab a top/bottom level ancestor with something like entry.ancestors.first/last.title.

But, say I'm at level 6, is it possible to grab an ancestor at a particular level, like entry.ancestors.level(3).title? Or would you have to grab all the ancestors and loop through them to accomplish this?

New to deep structures and just want to confirm I'm not missing something that would allow me to sidestep a loop. Thanks!

**Tags:** structure

---

## 1 answers

### Answer by username_1 on 2015-02-04. Score: 3 | Accepted answer

entry.ancestors returns an ElementCriteriaModel (→ getAncestors() in the Craft docs) prepped to return the entry's ancestors.
entry.ancestors.level(3) makes it return only entry ancestors which are at 3rd level of the structure.
As you well pointed out in the comments, an entry can only have one ancestor on a particular structure level. So you can now use the first() method to get this one entry. Keep in mind that if there's no ancestor at that level, the method won't return anything (null).
{% set ancestorL3 = entry.ancestors.level(3).first() %}

{% if ancestorL3 %}
    {{ ancestorL3.title }}
{% endif %}

> **Comments:**
> **username_0**: I see… I was thinking .level(3) would return a single entry as .first does (not sure how an entry could have more than one ancestor at a particular level). Thanks!  
> **username_1**: @alsophil male and female?? ...errrm no.  

---

