Yet another Menu Question

Hello

I Would like to implement a slightly changed treemenu. I would like to see all top-menu entries but only the active submenu and all subsubmenus of the active sub menu. If i am on the top level i can only see the topmenu entries.

Is this possible?
Thank you very much for your help. I am really new to Kirby and i didn’t find a solution yet.

Best regards
Florian

I’d change the treemenu snippet a little bit and then do the rest via CSS:

<?php if(!isset($subpages)) $subpages = $site->children() ?>
<ul class="treemenu">
  <?php foreach($subpages AS $p): ?>
  <li class="depth-<?php echo $p->depth() ?> <?php e($p->isActive(), ' active') ?>">
    <a href="<?php echo $p->url() ?>"><?php echo $p->title()->html() ?></a>
    <?php if($p->hasChildren()): ?>
    <?php snippet('treemenu', array('subpages' => $p->children())) ?>
    <?php endif ?>
  </li>
  <?php endforeach ?>
</ul>

CSS:

.treemenu li ul {
  display: none;
}
.treemenu li.active ul {
  display: block;
}
1 Like

Thank you very much! i test it first thing tomorrow :smile:

Thank you very much! It works as intended. Now i will have a look at how you did it :slight_smile: . the css part is no problem, the part with the php is a bit more tricky.
Best Regards
Florian

I did not change anything in the PHP code, I just added a class to the ul tag and moved the active class from the a tag to the li tag.

:blush: Thx for the explanation. :grinning:

I added back the part with the -> visible() methode. Now its perfect.

<?php foreach($subpages->visible() AS $p): ?>

Have a very nice day!

1 Like

Hello

I have found a solution that does everything without the CSS Part. Its a Treemenu that only shows the submenus and subsubmenus of the active main menu item. My approach was to change

<?php if($p->hasChildren()): ?>

to

<?php if($p->hasChildren() and $p->isOpen()): ?>

this has the advantage of only rendering what i really need. That is all the active Menu items and all sub- and subsubmenu items that are active. And it does not need any css. :blush:

Maybe someone need something like this too. so here is the full code:

<?php if(!isset($subpages)) $subpages = $site->children() ?>
<ul class="treemenu">
  <?php foreach($subpages->visible() AS $p): ?>
  <li class="depth-<?php echo $p->depth() ?> <?php e($p->isActive(), ' active') ?>">
	<a href="<?php echo $p->url() ?>"><?php echo $p->title()->html() ?></a>
    <?php if($p->hasChildren() and $p->isOpen()): ?>
    	<?php snippet('common/treemenu', array('subpages' => $p->children())) ?>    
	<?php endif ?>
  </li>
  <?php endforeach ?>
</ul>

have a nice weekend
Florian

Yeah, that’s much better … :slight_smile:

1 Like