How to create menu items based on taxonomy?

Hello,
im’ new to grav and don’t have specific skills in html or css so i used the SoraArticle skeleton for my vps hosted website.
I would like the “menu item” to display all pages with tag ‘Linux’, and i have read taxonomy related documentation and found this :

In your theme, you can easily display a list of pages that are written by ksmith by using taxonomy.findTaxonomy() to find them and iterate over them:

<h2>Kevin Smith's Posts</h2>
<ul>
{% for post in taxonomy.findTaxonomy({'author':'ksmith'}) %}
    <li>{{ post.title|e }}</li>
{% endfor %}
</ul>

i also found in page collections the following
By setting @taxonomy.tag: foo, Grav will find all the published pages in the /user/pages folder that have themselves set tag: foo in their taxonomy variable:

content:
   items:
      '@taxonomy.tag': foo

My question is which code should be used? and where do i have to put this code for menu item1 to display all pages related to tag Linux? should i have to create a yaml file or modify site.yaml in /user/config ?
I created a page called collection1 as the url for “menu item” and tried to paste that code in frontmatter but it displays syntax errors.

@igloo, Not sure if what you are trying to achieve is the way to go. Sure you can create a dropdown menu with a list of all Linux related pages, but depending on the amount of Linux related pages, that might not be the best UX.

If there are many Linux related pages, a better choice might be to create a blog page containing a collection of pages about Linux. You can see this in action in this Quark blog demo.

Anyway, to create a list of menu items based on a taxonomy collection, you should use the first code snippet and use it to replace lines 7-13 inside template /templates/partials/navigation.html.twig. Of course you’ll need to tweak the snippet to create a dropdown menu.

Blockquote but depending on the amount of Linux related pages, that might not be the best UX

Well , most of the pages and tutorials are related to Linux or SBC’s and there are a huge amount of pages.

Blockquote If there are many Linux related pages, a better choice might be to create a blog page containing a collection of pages about Linux

I will follow your advice since as i said i don’t have skills in html/css or web design.As i can see in the demo, the pages are displayed with tags related to photography or journal or any other tag. exp : https://demo.getgrav.org/blog-skeleton/**/tag:travel**
so i think i have to create page that will display when a tag is clicked. how can i do that? any link with step by step instructions?

Blockquote Anyway, to create a list of menu items based on a taxonomy collection…

Many thanks , i’ll try that for other pages with small amount like books or music related. That is exactly the type of step by step i was looking for.
Since i’m new to Grav, i think it’s a great CMS but not intended for newbies, we have to tweak manually yaml or twig files and not everything can be done from admin panel.

@igloo,

so i think i have to create page that will display when a tag is clicked. how can i do that? any link with step by step instructions?

Not quite… The blog page in the demo shows its content based on a url param eg. tag:journal. When clicking on a tag as displayed in plugin “Popular Tags” on the right side of the demo page, the page will refresh and show all blog items with the selected tag.

You can download skeleton Blog Site to see the demo in action and learn how the blog page has been constructed.

i think it’s a great CMS but not intended for newbies

IMHO Grav has been developed with developers in mind. The full power of Grav is available using a plain editor like vscode editing yaml, php and twig.
Admin has been added to ease the lifes of end-users/content creators. If you need to adapt the layout of a page beyond the build in options of a theme, you will soon be editing Twig, scss/css, or even php with a plain editor.

I finally found a solution to my newbie problem, by looking at the code in the skeleton and using docs about page collections. The menu item is now displaying all pages related to Linux by using taxonomy complex collections.
Many thanks for your time and patience.

2 Likes