<p>You can test for the entry's <code>level</code> property. Something like the following.</p>

<pre><code>{% if entry.level == 1 %}
    &lt;h1&gt;{{ entry.title }}&lt;/h1&gt;
{% else %}
    &lt;h1&gt;{{ entry.ancestors.first.title }}&lt;/h1&gt;
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-01-12T19:44:04.867Z
Ah, I didn't think about using level — that's a great solution. This is working, though I found that for the tertiary+ level we need "entry.ancestors.**first**.title" to get the right result … editing above.
philzelnar
CC BY-SA 3.0
2015-01-12T19:51:11.073Z
Great! I pulled this out of one of my own projects where the structure was limited to two levels. Couldn't remember whether '.first' or '.last' would get you to the top level. I updated the example to use 'first'.
Douglas McDonald
CC BY-SA 3.0
2015-01-12T19:10:38.480Z
2015-01-12T19:47:33.967Z
2015-01-12T19:47:33.967Z
Douglas McDonald
<p>Hi all… I have a Structure organized like this:</p>

<ol>
<li>Arts
<ul>
<li>Journals
<ul>
<li>Submissions</li>
</ul></li>
<li>Books</li>
</ul></li>
<li>Sciences
<ul>
<li>Journals
<ul>
<li>Submissions</li>
</ul></li>
<li>Books</li>
</ul></li>
</ol>

<p>…etc.</p>

<p>In my template, I want to grab the "Section" (e.g. Arts, Sciences) that I'm in and output it at the top of the page, both on the main page and subpages.</p>

<p>For decedents I can do that using:</p>

<pre><code>    {% set currentSubject = entry.getAncestors().level(1) %}
    {% for entry in currentSubject %}
    {{ entry.title }}
    {% endfor %}
</code></pre>

<p>But if I'm on the main page (e.g. Arts, Sciences) itself getAncestors won't help. Is there a smart conditional that will give me the title of the top level page regardless of if I'm on it or a descendent of it?</p>

<p>For some reason, I find myself hoping this can be done without looking at segments, but maybe that's the best route for this kind of thing?</p>

CC BY-SA 3.0
2015-01-12T17:14:56.033Z
I usually end up using segments for this, something like `{% set topLevel = craft.entries.slug(craft.request.segment(1)).first() %}`.
Steve Abraham
<structure>
Structure Nav: Output "Section" Title on both Landing and Decedents
# Structure Nav: Output "Section" Title on both Landing and Decedents

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

Hi all… I have a Structure organized like this:

Arts

Journals

Submissions

Books

Sciences

Journals

Submissions

Books


…etc.
In my template, I want to grab the "Section" (e.g. Arts, Sciences) that I'm in and output it at the top of the page, both on the main page and subpages.
For decedents I can do that using:
    {% set currentSubject = entry.getAncestors().level(1) %}
    {% for entry in currentSubject %}
    {{ entry.title }}
    {% endfor %}

But if I'm on the main page (e.g. Arts, Sciences) itself getAncestors won't help. Is there a smart conditional that will give me the title of the top level page regardless of if I'm on it or a descendent of it?
For some reason, I find myself hoping this can be done without looking at segments, but maybe that's the best route for this kind of thing?

**Tags:** structure
> **Comments:**
> **username_1**: I usually end up using segments for this, something like `{% set topLevel = craft.entries.slug(craft.request.segment(1)).first() %}`.  

---

## 1 answers

### Answer by username_2 on 2015-01-12. Score: 1 | Accepted answer

You can test for the entry's level property. Something like the following.
{% if entry.level == 1 %}
    {{ entry.title }}
{% else %}
    {{ entry.ancestors.first.title }}
{% endif %}

> **Comments:**
> **username_0**: Ah, I didn't think about using level — that's a great solution. This is working, though I found that for the tertiary+ level we need "entry.ancestors.**first**.title" to get the right result … editing above.  
> **username_2**: Great! I pulled this out of one of my own projects where the structure was limited to two levels. Couldn't remember whether '.first' or '.last' would get you to the top level. I updated the example to use 'first'.  

---

