1

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') %}


<ul class="list-unstyled">
  {% for entry in entries %}
 <li>
  <a href="{{ entry.url }}">{{ entry.title }}</a>
  {% 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 %}
 </li>
  {% endfor %}
<ul>
2

Found the answer in getSection()

<li><a href="{{ entry.url }}">{{ entry.title }}</a>

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

</li>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.