2

I'm a Craft first-timer building a simple blog site. I want to include breadcrumbs at the top of each page like so:

siteurl.com: [Blog]

siteurl.com/category/example-category: [Blog > Example Category]

siteurl.com/post/2015/entry-title: [Blog > Entry Title]

I've found this snippet which correctly outputs the breadcrumb for the entry permalink page:

<ul class="breadcrumbs">
    <li><a href="{{ siteUrl }}">Blog</a></li>
    {% if craft.request.segments %}
        {% for crumb in entry.getAncestors() %}
           <li>{{ crumb.getLink() }}</li>
        {% endfor %}
        <li>{{ entry.title() }}</a></li>
    {% endif %}
</ul>

But I also need something that will include a crumb for when the visitor is on a category page.

So far, my templates are structured like so:

templates/
 | index.html
 | _layout.html
 | posts/
    | index.html
    | category.html
    | _entry.html

Where the breadcrumb snippet is included in '_layout.html'.

In EE, I would just check my segments and use Low Seg2Cat to grab the category title. Is there an equivalent to this process in Craft? Or is it handled completely differently?

  • did you ever find a solution for this? – Katie Fritz Sep 2 '16 at 22:52
2

If your categories are only one level deep, you could hard-code the breadcrumbs into the category template:

{% block breadcrumb %}
  <ul class="breadcrumbs">
    <li><a href="{{ siteUrl }}">Blog</a></li>
    <li>{{ category.title }}</li>
  </ul>
{% endblock %}

In _layout.html you would have:

{% block breadcrumb %}
  <ul class="breadcrumbs">
    <li><a href="{{ siteUrl }}">Blog</a></li>
    {% if craft.request.segments %}
      {% for crumb in entry.getAncestors() %}
        <li>{{ crumb.getLink() }}</li>
      {% endfor %}
      <li>{{ entry.title() }}</a></li>
    {% endif %}
  </ul>
{% endblock %}

Haven't tried this myself, but I think it would work - the category template would override the layout.

Of course, if you have multiple levels of categories, the intermediate levels won't be included, so it's not an ideal solution.

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.