<p>You can use the <code>ancestors</code> property (which is a shortcut to <a href="http://buildwithcraft.com/docs/templating/entrymodel#getAncestors" rel="nofollow">getAncestors()</a>).</p>

<pre><code>{% set targetEntry = entry %}
{% if entry.level &gt; 1 %}
     {% set targetEntry = entry.ancestors.last %}
{% endif %}
</code></pre>

<p>To retrieve an ancestor with a specific level:</p>

<pre><code>{% set targetEntry = entry %}
{% for ancestorEntry in entry.ancestors %}
    {% if ancestorEntry.level == "2" %}
        {% set targetEntry = ancestorEntry %}
    {% endif %}
{% endfor %}
</code></pre>

<p>And if you want a specific distance (i.e. 2 levels above), you can use the distance property.</p>

<pre><code>{% set targetEntry = entry %}
{% if entry.level &gt; 1 %}
    {% set targetEntry = entry.ancestors(2).last %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-02-01T08:08:55.590Z
Thanks, works great on any of the sub-pages of the main ancestor/parent page, however when I visit the parent aka level 1 page nothing shows up :(
Mark H.
CC BY-SA 3.0
2015-02-01T08:34:17.093Z
Yes — If it's a level 1 entry then it has no ancestors. You would want to check the `level` or `entry.parent` or `entry.ancestors|length` first. Updated answer with example.
Douglas McDonald
CC BY-SA 3.0
2015-02-01T01:40:45.843Z
2015-02-01T08:33:27.210Z
2015-02-01T08:33:27.210Z
Douglas McDonald
<p>So, here is my final solution it looks ugly but works. I'm pretty sure that it could be done in a more effective / nicely looking way but this is my first website on Craft :)</p>

<pre><code>                        {% set topic = entry.ancestors.first %}
                        {% if topic %}
                            &lt;a href="{{ topic.url }}"&gt;{{ topic.title }}&lt;/a&gt;
                        {% else %}
                            {{ entry.title }}
                        {% endif %}
                        &lt;/h4&gt;
                        &lt;ul class="clearfix"&gt;
                        {% if entry.level &gt; 1 %}
                        {% for subsection in craft.entries.descendantOf(topic) if subsection.type != "state" %}
                            &lt;li class="icon-list"&gt;
                                &lt;a href="{{ subsection.url }}"&gt;{{ subsection.title }}&lt;/a&gt;
                            {% if craft.request.segment(1) == subsection.slug %}
                                &lt;span class="thumb-icon-list"&gt;&lt;i class="fa fa-caret-right"&gt;&lt;/i&gt;&lt;/span&gt;
                            {% endif %}
                            &lt;/li&gt;
                        {% endfor %}

                        {% else %}

                        {% for subsection in craft.entries.descendantOf(entry.id) if subsection.type != "state" %}
                            &lt;li class="icon-list"&gt;
                                &lt;a href="{{ subsection.url }}"&gt;{{ subsection.title }}&lt;/a&gt;
                            {% if craft.request.segment(1) == subsection.slug %}
                                &lt;span class="thumb-icon-list"&gt;&lt;i class="fa fa-caret-right"&gt;&lt;/i&gt;&lt;/span&gt;
                            {% endif %}
                            &lt;/li&gt;
                        {% endfor %}
                        {% endif %}
</code></pre>

CC BY-SA 3.0
2015-02-01T18:07:22.823Z
2015-02-01T18:07:22.823Z
Mark H.
<p>What I'm trying to do is to display child pages of a parent page. So far so good.</p>

<pre><code>                {% if entry.parent %}
                    &lt;div class="widget clearfix"&gt;
                        &lt;h4 class="widgettitle"&gt;&lt;a href="{{ entry.parent.url }}"&gt;{{ entry.parent.title }}&lt;/a&gt;&lt;/h4&gt;
                        &lt;ul class="clearfix"&gt;
                        {% set topic = entry.parent.id %}
                        {% for subsection in craft.entries.descendantOf(topic) if subsection.type != "state" %}
                            &lt;li class="cat-item"&gt;&lt;a href="{{ subsection.url }}"&gt;{{ subsection.title }}&lt;/a&gt;&lt;/li&gt;
                        {% endfor %}
                        &lt;/ul&gt;
                    &lt;/div&gt;
                {% endif %}
</code></pre>

<p>However if the sub page has another child page everything goes wrong as I see the results of the parent page that is a child itself.</p>

<p>How do  I ignore the hierarchy and display the results of level 1 aka grand parent page / topic?</p>

<p>Thanks.</p>

<navigation>
Easy Nav Question
# Easy Nav Question

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

What I'm trying to do is to display child pages of a parent page. So far so good.
                {% if entry.parent %}
                    
                        {{ entry.parent.title }}
                        
                        {% set topic = entry.parent.id %}
                        {% for subsection in craft.entries.descendantOf(topic) if subsection.type != "state" %}
                            {{ subsection.title }}
                        {% endfor %}
                        
                    
                {% endif %}

However if the sub page has another child page everything goes wrong as I see the results of the parent page that is a child itself.
How do  I ignore the hierarchy and display the results of level 1 aka grand parent page / topic?
Thanks.

**Tags:** navigation


---

## 2 answers

### Answer by username_1 on 2015-02-01. Score: 3

You can use the ancestors property (which is a shortcut to getAncestors()).
{% set targetEntry = entry %}
{% if entry.level > 1 %}
     {% set targetEntry = entry.ancestors.last %}
{% endif %}

To retrieve an ancestor with a specific level:
{% set targetEntry = entry %}
{% for ancestorEntry in entry.ancestors %}
    {% if ancestorEntry.level == "2" %}
        {% set targetEntry = ancestorEntry %}
    {% endif %}
{% endfor %}

And if you want a specific distance (i.e. 2 levels above), you can use the distance property.
{% set targetEntry = entry %}
{% if entry.level > 1 %}
    {% set targetEntry = entry.ancestors(2).last %}
{% endif %}

> **Comments:**
> **username_0**: Thanks, works great on any of the sub-pages of the main ancestor/parent page, however when I visit the parent aka level 1 page nothing shows up :(  
> **username_1**: Yes — If it's a level 1 entry then it has no ancestors. You would want to check the `level` or `entry.parent` or `entry.ancestors|length` first. Updated answer with example.  

---

### Answer by username_0 on 2015-02-01. Score: 0

So, here is my final solution it looks ugly but works. I'm pretty sure that it could be done in a more effective / nicely looking way but this is my first website on Craft :)
                        {% set topic = entry.ancestors.first %}
                        {% if topic %}
                            {{ topic.title }}
                        {% else %}
                            {{ entry.title }}
                        {% endif %}
                        
                        
                        {% if entry.level > 1 %}
                        {% for subsection in craft.entries.descendantOf(topic) if subsection.type != "state" %}
                            
                                {{ subsection.title }}
                            {% if craft.request.segment(1) == subsection.slug %}
                                
                            {% endif %}
                            
                        {% endfor %}

                        {% else %}

                        {% for subsection in craft.entries.descendantOf(entry.id) if subsection.type != "state" %}
                            
                                {{ subsection.title }}
                            {% if craft.request.segment(1) == subsection.slug %}
                                
                            {% endif %}
                            
                        {% endfor %}
                        {% endif %}


---

