keithmancuso / craft-menus

A simple but powerful menus plugin for Craft CMS built with a custom element type
51 stars 10 forks source link

Is it possible to create a breadcrumb based on the menu structure? #6

Open alexroper opened 9 years ago

alexroper commented 9 years ago

I'm trying to figure out if there's a way to create a breadcrumb navigation form a menu. I'd like to be able to search a menu's node for the current entry, and then get it's ancestors. Something like this:

<ul class="crumbs">
    {% for crumb in craft.menus.getNodes('mainMenu').relatedTo(entry).getAncestors() %}
        <li>{{ crumb.getLink() }}</li>
    {% endfor %}
</ul>

Similar to how you can output breadcrumbs based on a Structure: http://buildwithcraft.com/help/breadcrumbs

creativeorange commented 9 years ago

Hi Alex,

This idea might help you to get started:

<ul class="crumbs">
{% for node in craft.menus.getNodes('mainMenu') if node.active %}
    {% if node.level == 1 %}
        <li>{{ node.title }}</li>
    {% elseif node.level == 2 %}
        <li><a href="{{ node.parent.url }}">{{ node.parent }}</a></li>
        <li>{{ node.title }}</li>
    {% endif %}
{% endfor %}
</ul>
keithmancuso commented 8 years ago

Hey @alexroper curious if @creativeorange's solution worked for you?

alexroper commented 8 years ago

I did not end up using this solution because I needed a more flexible way to create breadcrumbs for content that's not included in the menu structure itself. I used a variation of this method from the Craft docs for Outputting a Custom List of Entries and creating an Entries field called "Breadcrumbs."