1

I have a a few pages outside of a structure but I want to display a breadcrumb for it as part of a structure. I am currently setting the parent page like so: {% set parent = craft.entries.section('pages').slug('phone-directory') %}

{{ parent.ancestors }}

But when I loop through to get the ancestors it is only getting the current parent not all ancestors? Any idea how to achieve this?

The structure is
Staff
    Phone Directory

The reason I am trying to do this, is the 3rd level pages are either category pages or is a single with some specific routes. I want to manually set the real parent in the template, so I can get the ancestors for the breadcrumb trail as though it is part of the same section.

How can I manually set the parent entry in the template and get the ancestors from this entry?

**** UPDATED ****
This is an update on the above to show my exact code and what isn't working and How I expect it work...If anyone ca shed some light?

So by default I am getting my breadcrumbs like so;

{% if entry is defined and entry.section.handle != 'homepage' %}
{% set crumbs =  parent.ancetors ?? entry.ancestors|default([]) %}
{% set crumbDivider = '<li><span class="mx-3 text-grey text-xs">/</span></li>' %}
<nav class="mb-8 mx-6">
  <ul class="list-reset flex flex-wrap items-center text-grey-dark text-sm">
    <li class="mr-3 text-xs text-grey-light">You are in:</li>
    <li><a href="{{ siteUrl }}">Home</a></li>
    {% for crumb in crumbs %}
    {{ crumbDivider|raw }}
    <li>{{ crumb.getLink() }}</li>
    {% endfor %}
    {{ crumbDivider|raw }}
    <li class="text-grey-darker">{{ entry.title ?? category.title }}</li>
  </ul>
</nav>
{% endif %}

This works as expect for normal standard entries. For example, within my Pages structure I have a parent of News with a Child of Internal News and on the Internal News page, the breadcrumb is reflected as; Home / News / SWH Internal News

Now on the news entry pages I want the breadcrumb to read as follows; Home / News / SWH Internal News / {{ Entry Title }}

I thought by setting the parent on the news/_entry.html template to the SWH Internal News page like so; {% set parent = craft.entries.section('pages').slug('archive') %}

Would then give me the ancestors of News / SWH Internal News but instead it only outputs SWH Internal News.

  • 1
    Can you add the breadcrumb loop code? It's hard to debug this without seeing the code that isn't working. – Simon Kuran Jun 4 '18 at 20:30
  • even just using {% set parent = craft.entries.section('pages').slug('phone-directory') %} {{ parent.ancestors }} only returns the one level though (Phone Directory). If I try {{ parent.first.ancestors }} I get the parent of the parent (Staff). The structure is: > Staff >>Phone Directory and this is the entry I am setting. I would expect this to then show the ancestors as Staff > Phone Directory – Terry Upton Jun 5 '18 at 6:56
  • 1
    @TerryUpton You'll need to iterate the parents of course – Robin Schambach Jun 5 '18 at 7:07
2

As @Robin Schambach said, you need to loop through the ancestors.

{% set parent = craft.entries.section('pages').slug('phone-directory') %}
{% for ancestor in parent.getAncestors() %}
    {{ ancestor.title }}
{% endfor %}

This won't output the Phone Directory entry since you're just looping through its ancestors. So to complete the breadcrumbs you'll need to add {{ parent.title }} after the loop.

  • Thanks for this but I am still a little confused as to why this isn't working like I expect. I have updated my answer to hopefully explain better. – Terry Upton Jun 6 '18 at 12:54
2

After much help and direction form @RobinSchambach (much appreciated). I have resolved this as follows;

{% set currentPage = craft.entries.section('pages').slug('archive').first() %}
{% set currentAncestors = currentPage.getAncestors.find() %}
{% set crumbs = currentAncestors|merge([currentPage]) %}

{% if entry is defined and entry.section.handle != 'homepage' %}
{% set object = crumbs ?? entry.ancestors  %}
{% set crumbs =  object %}
{% set crumbDivider = '<li><span class="mx-3 text-grey text-xs">/</span></li>' %}
<nav class="mb-8 mx-6">
  <ul class="list-reset flex flex-wrap items-center text-grey-dark text-sm">
    <li class="mr-3 text-xs text-grey-light">You are in:</li>
    <li><a href="{{ siteUrl }}">Home</a></li>
    {% for crumb in crumbs %}
    {{ crumbDivider|raw }}
    <li>{{ crumb.getLink() }}</li>
    {% endfor %}
    {{ crumbDivider|raw }}
    <li class="text-grey-darker">{{ entry.title ?? category.title }}</li>
  </ul>
</nav>
{% endif %}

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.