<p>Found the answer in <a href="https://craftcms.com/docs/templating/entrymodel#getSection" rel="nofollow noreferrer">getSection()</a></p>

<pre><code>&lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;

  {% set section = entry.getSection() %}
   {{ section }}
  {% set parent1 = entry.getAncestors() %}
   {% for item in parent1 %}
    / {{ item }}
   {% endear %}

&lt;/li&gt;
</code></pre>

CC BY-SA 3.0
2016-12-06T16:58:49.237Z
2016-12-06T17:08:23.613Z
2016-12-06T17:08:23.613Z
julzmon
<p>I'm having trouble getting the Structure parent title on a search results page.</p>

<p>I followed <a href="https://craftcms.stackexchange.com/questions/11614/title-of-the-current-parent-structure/11617#11617">these instructions</a> but I'm getting a "Impossible to access an attribute ("title") on a null variable" error.</p>

<p>In the following code the all ancestors do print except the first level Structure title.</p>

<pre><code>{% set query = craft.request.getParam('q') %}
{# {% set entries = craft.entries.search(query).order('score') %} #}
{% set entries = craft.entries.section().search(query).order('score') %}


&lt;ul class="list-unstyled"&gt;
  {% for entry in entries %}
 &lt;li&gt;
  &lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;
  {% set parent1 = entry.getAncestors().level(1) %}
  {% for item in parent1 %}
      / {{ item.title }}
    {% endfor %}
  {% set parent2 = entry.getAncestors().level(2) %}
  {% for item in parent2 %}
      / {{ item.title }}
    {% endfor %}
  {% set parent3 = entry.getAncestors().level(3) %}
  {% for item in parent3 %}
    / {{ item.title }}
  {% endfor %}
 &lt;/li&gt;
  {% endfor %}
&lt;ul&gt;
</code></pre>

<structure><search>
How to get Structure parent title and ancestors on search results page
# How to get Structure parent title and ancestors on search results page

(Asked by: username_0 on 2016-12-06)

I'm having trouble getting the Structure parent title on a search results page.
I followed these instructions but I'm getting a "Impossible to access an attribute ("title") on a null variable" error.
In the following code the all ancestors do print except the first level Structure title.
{% set query = craft.request.getParam('q') %}
{# {% set entries = craft.entries.search(query).order('score') %} #}
{% set entries = craft.entries.section().search(query).order('score') %}



  {% for entry in entries %}
 
  {{ entry.title }}
  {% set parent1 = entry.getAncestors().level(1) %}
  {% for item in parent1 %}
      / {{ item.title }}
    {% endfor %}
  {% set parent2 = entry.getAncestors().level(2) %}
  {% for item in parent2 %}
      / {{ item.title }}
    {% endfor %}
  {% set parent3 = entry.getAncestors().level(3) %}
  {% for item in parent3 %}
    / {{ item.title }}
  {% endfor %}
 
  {% endfor %}

**Tags:** structure, search


---

## 1 answers

### Answer by username_0 on 2016-12-06. Score: 2

Found the answer in getSection()
{{ entry.title }}

  {% set section = entry.getSection() %}
   {{ section }}
  {% set parent1 = entry.getAncestors() %}
   {% for item in parent1 %}
    / {{ item }}
   {% endear %}


---

