Answers.Body: <p>Something like this should do the trick (obviously, change <code>navStructureHandle</code> and <code>entriesFieldHandle</code> to real values):</p>
  
  <pre><code>{# Get the related nav entry for the current entry being viewed #}
  {% set navEntry = craft.entries.section('navStructureHandle').relatedTo({
      targetElement: entry,
      field: 'entriesFieldHandle'
  }).first() %}
  
  {# Get the root (top level parent) structure entry for the nav entry #}
  {% set rootEntry = navEntry ? craft.entries.ancestorOf(navEntry).level(1).first() : null %}
  
  {# If a root entry was found, print out the title #}
  {% if rootEntry %}
      &lt;span class="nav-header-title"&gt;{{ rootEntry.title }}&lt;/span&gt;
  {% endif %}
  </code></pre>
  
  <p>With the above, <code>rootEntry</code> will be <code>null</code> if the current entry doesn't have a related nav structure entry, or if a top level parent can't be found for the nav entry (i.e. the nav entry <em>is</em> top level).</p>
  
  <p>I wouldn't worry too much about the performance with the above, but if you do feel it's too complex for printing a header, you could consider using the <a href="https://github.com/aelvan/Preparse-Field-Craft" rel="nofollow">Preparse plugin</a> instead. Preparse basically lets you create a custom field containing Twig code that will be parsed whenever the entry is saved. This basically lets you "cache" the result of costly Twig operations to a plain text, custom field on an actual entry.</p>
  
  <p>Using Preparse, you could add a field called <code>navHeaderTitle</code> to your child entries (i.e. <em>not</em> the nav section). The field should contain the Twig code above, in addition to actually printing out the <code>rootEntry</code> title if a root entry is found (note that it's a good idea to wrap the whole thing in a <code>{% spaceless %}</code> tag pair):</p>
  
  <pre><code>{% spaceless %} 
      {% set navEntry = craft.entries.section('navStructureHandle').relatedTo({
      targetElement: entry,
      field: 'entriesFieldHandle'
  }).first() %}
      {% set rootEntry = navEntry ? craft.entries.ancestorOf(navEntry).level(1).first() : null %}
      {{ rootEntry ? rootEntry.title }}
  {% endspaceless %}
  </code></pre>
  
  <p>Then, you could simply do something like this in your template:</p>
  
  <pre><code>{# No need to pull any entries; just test for the `navHeaderTitle` field #}
  {% if entry.navHeaderTitle %}
      &lt;span class="nav-header-title"&gt;{{ entry.navHeaderTitle }}&lt;/span&gt;
  {% endif %}
  </code></pre>
  
  <p>Of course, the caveat with the Preparse approach is that if you ever change your root entry's title, you'll have to re-save all your child entries in order to re-parse the Twig and update the entries' <code>navHeaderTitle</code> value.</p>
  
  <p>A good compromise could be to use Preparse, but echo out the root entry ID instead of the title – then you could do this in your template:</p>
  
  <pre><code>{% set rootEntry = entry.navHeaderEntryId ? craft.entries.id(entry.navHeaderEntryId).first() : null %}
  {% if rootEntry %}
      &lt;span class="nav-header-title"&gt;{{ rootEntry.title }}&lt;/span&gt;
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-13T13:25:56.823Z
Answers.Text: Nope that's still null too .....Mmmmm
Answers.UserDisplayName: joomkit
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-13T13:32:11.813Z
Answers.Text: In a child entry template, that should definitely work, so there's something else going on. We're talking about a Structure section here, right? Not a Channel with an Entries field? You're also 100% sure that you're testing this on a child template, not the actual root/top level entry?
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-13T13:42:32.420Z
Answers.Text: Definitely a structure. I was putting this in an included file
  base
  ->includes/header.html
  site extends base
  but i just tried it all over and i still get null.....
Answers.UserDisplayName: joomkit
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-13T13:43:15.783Z
Answers.Text: Right, then I don't know. Just tested the above code and it works on my end.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-13T13:43:36.487Z
Answers.Text: Is the top level entry disabled, perhaps?
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-13T13:50:29.680Z
Answers.Text: Thanks for jumping on this. No top level is not disabled. It is odd.DUmping root entry in the entry template itself shows null too. MMmmmm
Answers.UserDisplayName: joomkit
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-13T14:06:59.027Z
Answers.Text: OK issue is that I am on entry page but I am trying to get the structure menu that points at this entry. I need to get the root parent for the structure I am in......
Answers.UserDisplayName: joomkit
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-13T19:05:53.117Z
Answers.Text: @joomkit Your updated question included lots of info that was basically impossible to know about reading it initially, but I think I get your issue now – see above for an updated answer.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-13T13:08:18.053Z
Answers.LastActivityDate: 2016-07-13T19:24:23.677Z
Answers.LastEditDate: 2016-07-13T19:24:23.677Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>I am puzzling the best way to implement hero images per 'section' of a site.
  I can set a global default hero image no problem. But I want to set one header image for the root and children of a particular section.
  I can't use url segments as the site is multi-lingual and it will change.</p>
  
  <p>I've tried detecting the root of the current structure to base my template code on. </p>
  
  <pre><code>{% set rootEntry = entry.getAncestors().first() %}
  </code></pre>
  
  <p>But this returns null - like there is no model returned.</p>
  
  <p>UPDATE:
  NO answer yet but a repharasing of the question.
  IN order to do this you need to get the top level entry based on where you are in the site structure/tree. This link <a href="https://craftcms.stackexchange.com/questions/6995/building-a-nav-from-the-top-level-page-of-a-structure/6997#6997">Building a nav from the top level page of a structure</a> demonstrates great working code.</p>
  
  <p>However the assumption in the above posts and many others is that the structure is built out of the actual entry pages themselves.</p>
  
  <p>I have two structures. Pages and menus. The menu structure has related entry fields that 'point' to the pages.</p>
  
  <p>So in in my scenario the menu structure is built out of related entry fields - not the entry being viewed. Hence, the reasons getAncestors was not working was because it was taking the existing entry a visitor was viewing and looking for a hierarchy structure that was flat.</p>
  
  <p>So the question is now more complex - how do you get the top level tree branch of your structure menu based on the current entry viewed when that current entry is a related entry field in your structure menu? It seems very complicated to achieve. Especially just for flexible headers per sections....</p>
  
  <p>FINAL UPDATE
  Thanks Mats - i couldn't have done this without your diligent replies.
  Below is my final solution to find out what the top level root is. It is expressed as sub navigation for structure menu but it does two things - build s the sub nav and identifies the root of the tree where you are. NB macro is used for active links. </p>
  
  <pre><code>{# Get the sidebar structure subnavigation #}
  {# check for entry so channels do not break #}
  {% if entry is defined %}
      {% set navEntry = craft.entries.section('menu').relatedTo({
      targetElement: entry,
      field: 'entryLink'
      }).first() %}
  
      {# get the root entry of the structure set to null if we are at top #}
      {% set root = navEntry ? craft.entries.ancestorOf(navEntry).level(1).first() : null %}
  
      {# setup variables in switch for ouput of heading and sub links#}
      {% switch root %}
          {% case null %}
              {% if navEntry.hasDescendants() %}
                  {% set heading = navEntry.title %}
                  {% set thisSubMenu = craft.entries.descendantOf(navEntry) %}
              {% endif %}
              {% set rootEntry = navEntry %}
          {% case true %}
              {% set heading = root.title %}
              {% set thisSubMenu = craft.entries.descendantOf(root) %}
              {% set rootEntry = root %}
      {% endswitch %}
  {% endif %}
  
  {% if heading is defined %}
      &lt;h3&gt;{{ heading }}&lt;/h3&gt;
  {% endif %}
  
  {# Get entries to build the sub-menu #}
  {% if thisSubMenu is defined %}
  {% set subMenu = thisSubMenu %}
  &lt;ul&gt;
      {% for entry in subMenu %}
  
          {% if entry.entryLink.first|length %}
  
              {% set subNavLink = entry.entryLink.first().uri %}
  
          {% elseif entry.customMenuUrl|length %}
  
              {% set subNavLink = entry.customMenuUrl %}
  
      {% endif %}
      &lt;li&gt;
          &lt;a  href="{{ siteUrl }}{{ subNavLink }}" class="{{ macros.isActiveChildClass(entry.slug) | trim }} level{{ entry.level }}"&gt;{{ entry.title }}&lt;/a&gt;
      &lt;/li&gt;
      {% endfor %}
  &lt;/ul&gt;
  </code></pre>
  
  <p>{% endif %}</p>
  
Tags: <structure>
Title: How to set hero image for root and children in a structure
ThreadText: # How to set hero image for root and children in a structure
  
  (Asked by: username_0 on 2016-07-13)
  
  I am puzzling the best way to implement hero images per 'section' of a site.
  I can set a global default hero image no problem. But I want to set one header image for the root and children of a particular section.
  I can't use url segments as the site is multi-lingual and it will change.
  
  I've tried detecting the root of the current structure to base my template code on.
  
  ```
  {% set rootEntry = entry.getAncestors().first() %}
  
  ```
  
  But this returns null - like there is no model returned.
  
  UPDATE:
  NO answer yet but a repharasing of the question.
  IN order to do this you need to get the top level entry based on where you are in the site structure/tree. This link [Building a nav from the top level page of a structure](https://craftcms.stackexchange.com/questions/6995/building-a-nav-from-the-top-level-page-of-a-structure/6997#6997) demonstrates great working code.
  
  However the assumption in the above posts and many others is that the structure is built out of the actual entry pages themselves.
  
  I have two structures. Pages and menus. The menu structure has related entry fields that 'point' to the pages.
  
  So in in my scenario the menu structure is built out of related entry fields - not the entry being viewed. Hence, the reasons getAncestors was not working was because it was taking the existing entry a visitor was viewing and looking for a hierarchy structure that was flat.
  
  So the question is now more complex - how do you get the top level tree branch of your structure menu based on the current entry viewed when that current entry is a related entry field in your structure menu? It seems very complicated to achieve. Especially just for flexible headers per sections....
  
  FINAL UPDATE
  Thanks username_1 - i couldn't have done this without your diligent replies.
  Below is my final solution to find out what the top level root is. It is expressed as sub navigation for structure menu but it does two things - build s the sub nav and identifies the root of the tree where you are. NB macro is used for active links.
  
  ```
  {# Get the sidebar structure subnavigation #}
  {# check for entry so channels do not break #}
  {% if entry is defined %}
      {% set navEntry = craft.entries.section('menu').relatedTo({
      targetElement: entry,
      field: 'entryLink'
      }).first() %}
  
      {# get the root entry of the structure set to null if we are at top #}
      {% set root = navEntry ? craft.entries.ancestorOf(navEntry).level(1).first() : null %}
  
      {# setup variables in switch for ouput of heading and sub links#}
      {% switch root %}
          {% case null %}
              {% if navEntry.hasDescendants() %}
                  {% set heading = navEntry.title %}
                  {% set thisSubMenu = craft.entries.descendantOf(navEntry) %}
              {% endif %}
              {% set rootEntry = navEntry %}
          {% case true %}
              {% set heading = root.title %}
              {% set thisSubMenu = craft.entries.descendantOf(root) %}
              {% set rootEntry = root %}
      {% endswitch %}
  {% endif %}
  
  {% if heading is defined %}
      <h3>{{ heading }}</h3>
  {% endif %}
  
  {# Get entries to build the sub-menu #}
  {% if thisSubMenu is defined %}
  {% set subMenu = thisSubMenu %}
  <ul>
      {% for entry in subMenu %}
  
          {% if entry.entryLink.first|length %}
  
              {% set subNavLink = entry.entryLink.first().uri %}
  
          {% elseif entry.customMenuUrl|length %}
  
              {% set subNavLink = entry.customMenuUrl %}
  
      {% endif %}
      <li>
          <a  href="{{ siteUrl }}{{ subNavLink }}" class="{{ macros.isActiveChildClass(entry.slug) | trim }} level{{ entry.level }}">{{ entry.title }}</a>
      </li>
      {% endfor %}
  </ul>
  
  ```
  
  {% endif %}
  
  **Tags:** structure
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-07-13. Score: 1 | Accepted answer
  
  Something like this should do the trick (obviously, change `navStructureHandle` and `entriesFieldHandle` to real values):
  
  ```
  {# Get the related nav entry for the current entry being viewed #}
  {% set navEntry = craft.entries.section('navStructureHandle').relatedTo({
      targetElement: entry,
      field: 'entriesFieldHandle'
  }).first() %}
  
  {# Get the root (top level parent) structure entry for the nav entry #}
  {% set rootEntry = navEntry ? craft.entries.ancestorOf(navEntry).level(1).first() : null %}
  
  {# If a root entry was found, print out the title #}
  {% if rootEntry %}
      <span class="nav-header-title">{{ rootEntry.title }}</span>
  {% endif %}
  
  ```
  
  With the above, `rootEntry` will be `null` if the current entry doesn't have a related nav structure entry, or if a top level parent can't be found for the nav entry (i.e. the nav entry *is* top level).
  
  I wouldn't worry too much about the performance with the above, but if you do feel it's too complex for printing a header, you could consider using the [Preparse plugin](https://github.com/aelvan/Preparse-Field-Craft) instead. Preparse basically lets you create a custom field containing Twig code that will be parsed whenever the entry is saved. This basically lets you "cache" the result of costly Twig operations to a plain text, custom field on an actual entry.
  
  Using Preparse, you could add a field called `navHeaderTitle` to your child entries (i.e. *not* the nav section). The field should contain the Twig code above, in addition to actually printing out the `rootEntry` title if a root entry is found (note that it's a good idea to wrap the whole thing in a `{% spaceless %}` tag pair):
  
  ```
  {% spaceless %} 
      {% set navEntry = craft.entries.section('navStructureHandle').relatedTo({
      targetElement: entry,
      field: 'entriesFieldHandle'
  }).first() %}
      {% set rootEntry = navEntry ? craft.entries.ancestorOf(navEntry).level(1).first() : null %}
      {{ rootEntry ? rootEntry.title }}
  {% endspaceless %}
  
  ```
  
  Then, you could simply do something like this in your template:
  
  ```
  {# No need to pull any entries; just test for the `navHeaderTitle` field #}
  {% if entry.navHeaderTitle %}
      <span class="nav-header-title">{{ entry.navHeaderTitle }}</span>
  {% endif %}
  
  ```
  
  Of course, the caveat with the Preparse approach is that if you ever change your root entry's title, you'll have to re-save all your child entries in order to re-parse the Twig and update the entries' `navHeaderTitle` value.
  
  A good compromise could be to use Preparse, but echo out the root entry ID instead of the title – then you could do this in your template:
  
  ```
  {% set rootEntry = entry.navHeaderEntryId ? craft.entries.id(entry.navHeaderEntryId).first() : null %}
  {% if rootEntry %}
      <span class="nav-header-title">{{ rootEntry.title }}</span>
  {% endif %}
  
  ```
  
  > **Comments:**
  > **username_0**: Nope that's still null too .....Mmmmm  
  > **username_1**: In a child entry template, that should definitely work, so there's something else going on. We're talking about a Structure section here, right? Not a Channel with an Entries field? You're also 100% sure that you're testing this on a child template, not the actual root/top level entry?  
  > **username_0**: Definitely a structure. I was putting this in an included file base ->includes/header.html site extends base but i just tried it all over and i still get null.....  
  > **username_1**: Right, then I don't know. Just tested the above code and it works on my end.  
  > **username_1**: Is the top level entry disabled, perhaps?  
  > **username_0**: Thanks for jumping on this. No top level is not disabled. It is odd.DUmping root entry in the entry template itself shows null too. MMmmmm  
  > **username_0**: OK issue is that I am on entry page but I am trying to get the structure menu that points at this entry. I need to get the root parent for the structure I am in......  
  > **username_1**: @username_0 Your updated question included lots of info that was basically impossible to know about reading it initially, but I think I get your issue now – see above for an updated answer.  
  
  ---
  
  