I have a structure set up like:

What we do (set as a structure)

  • 2nd level page A

    * 3rd level page A
    * 3rd level page A
  • 2nd level page B

    * 3rd level page B

I'm setting up breadcrumbs for this so it's

Home / What we do / 2nd level page A / 3rd level page A

This is what I have so far but I can't figure out how to get the entry section url working (what-we-do) which is set in the Entry Url format.

{% set crumbs = entry.ancestors|default([]) %}
{% if crumbs|length %}
<ul class="breadcrumbs">
    <li><a href="{{ siteUrl }}">Home</a></li>
    <li><a href="{{ NEED HELP }}"> {{ entry.section.name }}</a></li>
    {% for crumb in crumbs %}
        <li><a href="{{ crumb.url }}">{{ crumb.title }}</a></li>
    {% endfor %}
    <li>{{ entry.title }}</li>
{% endif %}
  • 1
    What does entry.url give you? – Luke Pearce Jul 21 '16 at 16:12
  • The whole URL. I'm after the url that the structure adds to the level above the entry. – sarah3585 Jul 22 '16 at 9:44
  • I'm not sure what you mean? Using {{ entry.url }} will link to the section name you output with {{ entry.section.name }}. – Luke Pearce Jul 22 '16 at 9:49
  • {{ entry.url }} gives you the url of the current entry. ie /what-we-do/2nd-level/3rd-level I need just the /what-we-do part for the breadcrumb – sarah3585 Jul 22 '16 at 10:47

I think you've made this more complicated that it needs to be for yourself. Reading the Craft docs on Outputting the Entry’s Ancestors in a Structure Section it looks like you can use the handle .getAncestors() method to output the entries leading up to the currently viewed entry automatically, and have access to their URLs.

Give the example a go.

{% if entry.level > 1 %}
  <ul class="crumbs">
    {% for crumb in entry.getAncestors() %}
      <li>{{ crumb.getLink() }}</li>
    {% endfor %}
{% 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.