Dynamic Menu in Bolt
When I setup this site, I really wanted to try out bolt. I am really glad I chose this over Wordpress for many reasons, but one reason above all else. The community. Take today for instance, I wanted to modify my bolt install.
Bolt has a somewhat hardcoded menu system which at very was great. But once I started using it, it's a bit of a pain. After every blog post I would have to and update the menu. Surely there was to be a better way.
Dynamic Menu
In our Menu.yml
- label: PHP Articles
path: pages
list:
contenttype: pages
taxonomytype: chapters
taxonomy: php
class: last
And we need to modify our _sub_menu.twig
{% set list = [] %}
{% if item.list|default(false) %}
{% setcontent listedcontent = item.list.contenttype where {(item.list.taxonomytype) : (item.list.taxonomy)} %}
{% for listitem in listedcontent %}
{% set list = list|merge([{title: listitem.gettitle(), link: listitem.link, label: listitem.gettitle()}]) %}
{% endfor %}
<ul class="dropdown">
{% for submenu in list %}
{{ display_menu_item(submenu, loop) }}
{% endfor %}
</ul>
{% else %}
<ul class="dropdown">
{% for submenu in item.submenu %}
{{ display_menu_item(submenu, loop) }}
{% endfor %}
</ul>
{% endif %}
BAM!
Now all new posts tagged with my PHP taxonomy will now show up in my wonderful pretty menu!