Dropdown Menu Item

Hi, I want to create a simple menu with 3 links. One of them should be a drop-down with links to each subpage.
The structure should be like this:

  • LINK

  • LINK (should only show the page name / should not link to this page)

    • Link to child
    • Link to child
    • Link to child
  • Link

I tried many different options but couldnt find a solution which only shows the pagename for the dropdown link.

<header>
	<?php $items = $pages->visible(); if($items->count()): ?>
		<nav>
		    <?php foreach($pages->visible() as $item): ?>
		    <li <?php e($item->isOpen(), ' class="active" ') ?> >
		      <a href="<?= $item->url() ?>"><?= $item->title()->html() ?></a>
		  
		        <?php if($item->hasChildren()): ?>
			        <ul class="sublink">
			            <?php foreach($item->children() as $child): ?>
			              <li><a href="<?= $child->url() ?>"><?= $child->title()->html() ?></a></li>
			            <?php endforeach ?>
			        </ul>
		        <?php endif ?>
		        
		    </li>
		    <?php endforeach ?>
		</nav>
	<?php endif ?>
</header>

This should output what you want:

<header>
  <?php $items = $pages->visible(); if($items->count()): ?>
    <nav>
      <?php foreach($items as $item): ?>
        <li <?php e($item->isOpen(), ' class="active" ') ?> >   
          <?php if($item->hasChildren()): ?>
            <?= $item->title()->html() ?>
            <ul class="sublink">
              <?php foreach($item->children() as $child): ?>
                <li><a href="<?= $child->url() ?>"><?= $child->title()->html() ?></a></li>
              <?php endforeach ?>
            </ul>
          <?php else: ?>
            <a href="<?= $item->url() ?>"><?= $item->title()->html() ?></a>
          <?php endif ?>
        </li>
      <?php endforeach ?>
    </nav>
  <?php endif ?>
</header>
1 Like

is working perfectly… .thank you :slight_smile:

<?php $items = $pages->visible(); if($items->count()): ?>
  <nav>
      <?php foreach($pages->visible() as $item): ?>

Note that you defined $items but then didn’t use that variable at all…

1 Like

Uh I see … Thanks again!!!

Is there an example on how to append the “active” class without writing the whole class="active"

also is the e() a CakePHP function? is this still best practice?

e() is a Kirby helper that echos based on the result of an if set as the first parameter. See here for the Kirby 2 version, but it is also possible with Kirby 3.

Thanks for the feed back…

I should be able to do something like this

$classList = ['myClass', 'secondaryClass'];
if ($item->isOpen()){
    $classList[] = 'active';
}

<li class="<?= implode(' ', $classList) ?>">

I’ll give it a try!

Should be

<li class="<?= implode(' ', $classList) ?>">