Side menu disappears

Hi,

I am trying to figure out what I might be doing wrong. I have a side menu with children pages as links. When I click on a child link the side menu disappears from the page but the contents displayed on the page.

This is the side menu.

      <?php foreach ($page->children() as $sidemenu): ?>
                <a href="<?= $sidemenu->url()?>">
                     <?= $sidemenu->title() ?>
                </a>
      <?php endforeach ?>

If you are on a child page, there are no children anymore, do nothing will be displayed. When calling the snippet, you have to pass the correct collection of pages as parameter.

$page always refers to the current page.

Is that your complete menu, no nav tag around it?

I have tags around it (un-order list).

I am still learning Kirby, so I should use pages? I am not fully understanding, it’s not you you are very knowledgeable with using Kirby. I tried using pages it showed all the pages, but when I click on a link the side menu did not disappears, getting close.

<?php foreach ($pages->children() as $sidemenu): ?>
      <a href="<?= $sidemenu->url()?>">
           <?= $sidemenu->title() ?> 
      </a> 
<?php endforeach ?>

I wish I could be really good at Kirby hopefully one day there will be some training here in the US.

Don’t worry, we are here to help.

The $pages variable will output all main pages, that’s not what you want:

  1. Put your menu in a snippet, e.g. sidemenu.php
<?php if($menuItems->count()): `>
<nav>
    <ul>
        <?php foreach ($menuItems as $menuItem): ?>
            <li> 
               <a href="<?= $menuItem->url()?>">
                   <?= $menuItem->title() ?> 
               </a> 
            </li>
        <?php endforeach ?>
    </ul>
</nav>
<?php endif ?>

Then in your main page, call this snippet like this:

<?php snippet('sidemenu', ['menuItems' => $page->children()]) ?>

In the template for the children pages, like this:

<?php snippet('sidemenu', ['menuItems' => $page->siblings()]) ?>

Also check out the objects that are available in templates by default and what they mean: Kirby's PHP API | Kirby CMS

1 Like

@tenixe Thank you so much for your help and patient. And thank you for the link for object url. If you have any recommendation for me to better understand Kirby please share.

The guide in general should be a good starting point. Also, I have recently added comments to the Starterkit, don’t know if you have seen that?

Apart from that, learning by doing is probably the best approach. And just testing things, what happens when I change this or that, etc.?