Conditional Menu

Hi there! I’m new to kirby. By now, just testing it and try to understand how it works. I’m mocking up a testing local site, to collect all my varnish recipes.
The idea is to have many recipes, which has a “Kind” field which can be any of three values: “Varnish”, “Ground” or “Stain”.

I’d like to store all them in the same recipes folder, but I think that it will be awesome to have a separate menu for each kind, so to have all varnishes shows in Varnish, grounds in Grounds and Stains in stain.

Any one could point me to the correct way to implement it?


If you got your sites with a field kind you can filter them for the menu:

$varnishPages = $pages->filterBy('kind', 'Varnish');

To get only the visible pages:

$varnishPages = $pages->filterBy('kind', 'Varnish')->visible();

Then $varnishPages is a pages collection that you can use like $pages, e.g.

<nav id="varnish-menu">
    <?php foreach ($varnishPages as $p): ?>
      <li><a href="<?php echo $p->url() ?>"><?php echo $p->title()->html() ?></a></li>
    <?php endforeach ?> 

Actually this is untested but should work an give you a good starting point. For more see the docs and especially the cheat sheet about page and pages methods.

1 Like

Awesome! Definetely simple to understand… I’m confident I can proceed!!!