# Nested Menu - sub menu items not collasping

**URL:** <https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634>\
**Category:** Questions\
**Tags:** v3\
**Created:** [May 23, 2023, 12:42pm UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634 "2023-05-23T12:42:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidroberts](https://avatars.discourse-cdn.com/v4/letter/d/3da27b/32.png) [@davidroberts](https://forum.getkirby.com/u/davidroberts)\
**Post date:** [May 23, 2023, 12:42pm UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/1 "2023-05-23T12:42:05Z")

</div>

I am using the code from the [Nested menu](https://getkirby.com/docs/cookbook/templating/menus#nested-menu) page as per below. The nested menu items do not collapse under the main heading for the section. So you have Services | Mindfulness | Therapy-services all on the same level. How do I fix this problem? Thanks.

 ![CleanShot 2023-05-23 at 1.32.09](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/5/5d86bdc37cbf9d91cad4db9cd3752d7f71133bff.jpeg)

```auto
 <nav class="nav-collapse">
		<ul class="header-links">
		  <?php foreach($items as $item): ?>
			<li>
			  <a<?php e($item->isOpen(), 'class="active"') ?> href="<?= $item->url() ?>"><?= $item->title()->html() ?></a>
	
				<?php
				// get all children for the current menu item
				
				  $children = $item->children()->listed();
	
				  // display the submenu if children are available
				  if ($children->isNotEmpty()) :
				  ?>+<ul class="sub-menu">
					  <?php foreach ($children as $child) : ?>
						<li>
						  <a<?php e($child->isOpen(), ' class="active"') ?> href="<?= $child->url() ?>"><?= $child->title()->html() ?></a>
						</li>
					  <?php endforeach ?>
					</ul>
				  <?php endif ?>
				
			</li>
		  <?php endforeach ?>
		</ul>
	  </nav>

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 23, 2023, 12:52pm UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/2 "2023-05-23T12:52:33Z")

</div>

This is just code that doesn’t come with any styling, so you have to take care of this yourself.

---

<div class="post-metadata">

**Author:** ![davidroberts](https://avatars.discourse-cdn.com/v4/letter/d/3da27b/32.png) [@davidroberts](https://forum.getkirby.com/u/davidroberts)\
**Post date:** [May 23, 2023, 3:05pm UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/3 "2023-05-23T15:05:45Z")

</div>

If I was going to use something like this [CSS-Only Accessible Dropdown Navigation Menu | Modern CSS Solutions](https://moderncss.dev/css-only-accessible-dropdown-navigation-menu/) how do I know where to markup the code if I have two separate sub-menus using the code above?

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [May 23, 2023, 3:44pm UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/4 "2023-05-23T15:44:13Z")

</div>

Not tested but i think something like this should do it:

```php
<nav aria-label="Main Navigation">
	<ul>
		<?php foreach ($items as $item) : ?>

			<?php
			// get all children for the current menu item

			$children = $item->children()->listed();

			// display the submenu if children are available
			if ($children->isNotEmpty()): ?>

				<li class="dropdown">
					<!-- aria-expanded needs managed with Javascript -->
					<button <?php e($item->isOpen(), 'class="active"') ?> type="button" class="dropdown__title" aria-expanded="false" aria-controls="<?= $item->title()->html() ?>-dropdown">

						<?= $item->title()->html() ?>
					</button>

					<ul class="dropdown__menu" id="="<?= $item->title()->html() ?>-dropdown"">
						<?php foreach ($children as $child) : ?>
							<li>
								<a<?php e($child->isOpen(), ' class="active"') ?> href="<?= $child->url() ?>"><?= $child->title()->html() ?></a>
							</li>
						<?php endforeach ?>
					</ul>
				</li>

			<?php else : ?>

			<li>
				<a<?php e($item->isOpen(), 'class="active"') ?> href="<?= $item->url() ?>"><?= $item->title()->html() ?></a>
			</li>

			<?php endif ?>
		<?php endforeach ?>
	</ul>
</nav>

```

---

<div class="post-metadata">

**Author:** ![davidroberts](https://avatars.discourse-cdn.com/v4/letter/d/3da27b/32.png) [@davidroberts](https://forum.getkirby.com/u/davidroberts)\
**Post date:** [May 23, 2023, 4:35pm UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/5 "2023-05-23T16:35:18Z")

</div>

Thanks, but it does not work. Loads code into the page output.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 23, 2023, 4:39pm UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/6 "2023-05-23T16:39:33Z")

</div>

There’s just a PHP opening tag missing in the button, should be

```auto
<button <?php e($item->isOpen(), 'class="active"') ?> type="button" class="dropdown__title" aria-expanded="false" aria-controls="<?= $item->title()->html() ?>-dropdown">

```

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [May 23, 2023, 6:22pm UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/7 "2023-05-23T18:22:01Z")

</div>

Oh whoops! i wondered why it colured half of it blue! sorry about that, i couldnt spot the mistake before i had to pop out for a while.

---

<div class="post-metadata">

**Author:** ![davidroberts](https://avatars.discourse-cdn.com/v4/letter/d/3da27b/32.png) [@davidroberts](https://forum.getkirby.com/u/davidroberts)\
**Post date:** [May 24, 2023, 9:10am UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/8 "2023-05-24T09:10:03Z")

</div>

Hi,

Thanks for your help. I added the code but I get an offline message. The code now is:

```auto
<nav aria-label="Main Navigation">
	<ul>
		<?php foreach ($items as $item) : ?>

			<?php
			// get all children for the current menu item

			$children = $item->children()->listed();

			// display the submenu if children are available
			if ($children->isNotEmpty()): ?>

				<li class="dropdown">
					<!-- aria-expanded needs managed with Javascript -->
					<button <?php e($item->isOpen(), 'class="active"') ?> type="button" class="dropdown__title" aria-expanded="false" aria-controls="<?= $item->title()->html() ?>-dropdown">
						<?= $item->title()->html() ?>
					</button>

					<ul class="dropdown__menu" id="="<?= $item->title()->html() ?>-dropdown"">
						<?php foreach ($children as $child) : ?>
							<li>
								<a<?php e($child->isOpen(), ' class="active"') ?> href="<?= $child->url() ?>"><?= $child->title()->html() ?></a>
							</li>
						<?php endforeach ?>
					</ul>
				</li>

			<?php else : ?>

			<li>
				<a<?php e($item->isOpen(), 'class="active"') ?> href="<?= $item->url() ?>"><?= $item->title()->html() ?></a>
			</li>

			<?php endif ?>
		<?php endforeach ?>
	</ul>
</nav>

```

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [May 24, 2023, 9:33am UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/9 "2023-05-24T09:33:34Z")

</div>

> [@davidroberts](#):
>
> but I get an offline message

Follow the instructions in the message and report the error that shows up.

---

<div class="post-metadata">

**Author:** ![davidroberts](https://avatars.discourse-cdn.com/v4/letter/d/3da27b/32.png) [@davidroberts](https://forum.getkirby.com/u/davidroberts)\
**Post date:** [May 24, 2023, 11:54am UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/10 "2023-05-24T11:54:09Z")

</div>

Follow the instructions in the message and report the error that shows up.

I don’t see any instructions.

The message I get is:  
This page is currently offline due to an unexpected error. We are very sorry for the inconvenience and will fix it as soon as possible.

Advice for developers and administrators:  
Enable [debug mode](https://getkirby.com/docs/reference/system/options/debug) to get further information about the error.

---

<div class="post-metadata">

**Author:** ![davidroberts](https://avatars.discourse-cdn.com/v4/letter/d/3da27b/32.png) [@davidroberts](https://forum.getkirby.com/u/davidroberts)\
**Post date:** [May 24, 2023, 11:57am UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/11 "2023-05-24T11:57:33Z")

</div>

Whoops\Exception\ErrorException thrown with message “Undefined variable $items”

Stacktrace:  
#19 Whoops\Exception\ErrorException in /Users/dr/sites/localhost/Mindful/site/snippets/header.php:12  
#18 Whoops\Run:handleError in /Users/dr/sites/localhost/Mindful/site/snippets/header.php:12  
#17 include in /Users/dr/sites/localhost/Mindful/kirby/src/Filesystem/F.php:425  
#16 Kirby\Filesystem\F:loadIsolated in /Users/dr/sites/localhost/Mindful/kirby/src/Filesystem/F.php:364  
#15 Kirby\Filesystem\F:Kirby\Filesystem{closure} in /Users/dr/sites/localhost/Mindful/kirby/src/Filesystem/F.php:372  
#14 Kirby\Filesystem\F:load in /Users/dr/sites/localhost/Mindful/kirby/src/Toolkit/Tpl.php:34  
#13 Kirby\Toolkit\Tpl:load in /Users/dr/sites/localhost/Mindful/kirby/src/Template/Snippet.php:173  
#12 Kirby\Template\Snippet:factory in /Users/dr/sites/localhost/Mindful/kirby/config/components.php:284  
#11 Kirby\Cms\Core:{closure} in /Users/dr/sites/localhost/Mindful/kirby/src/Cms/App.php:1618  
#10 Kirby\Cms\App:snippet in /Users/dr/sites/localhost/Mindful/kirby/config/helpers.php:515  
#9 snippet in /Users/dr/sites/localhost/Mindful/site/templates/home.php:2  
#8 include in /Users/dr/sites/localhost/Mindful/kirby/src/Filesystem/F.php:425  
#7 Kirby\Filesystem\F:loadIsolated in /Users/dr/sites/localhost/Mindful/kirby/src/Filesystem/F.php:364  
#6 Kirby\Filesystem\F:Kirby\Filesystem{closure} in /Users/dr/sites/localhost/Mindful/kirby/src/Filesystem/F.php:372  
#5 Kirby\Filesystem\F:load in /Users/dr/sites/localhost/Mindful/kirby/src/Toolkit/Tpl.php:34  
#4 Kirby\Toolkit\Tpl:load in /Users/dr/sites/localhost/Mindful/kirby/src/Template/Template.php:155  
#3 Kirby\Template\Template:render in /Users/dr/sites/localhost/Mindful/kirby/src/Cms/Page.php:1071  
#2 Kirby\Cms\Page:render in /Users/dr/sites/localhost/Mindful/kirby/src/Cms/App.php:796  
#1 Kirby\Cms\App:io in /Users/dr/sites/localhost/Mindful/kirby/src/Cms/App.php:1235  
#0 Kirby\Cms\App:render in /Users/dr/sites/localhost/Mindful/index.php:5

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [May 24, 2023, 12:03pm UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/12 "2023-05-24T12:03:33Z")

</div>

> [@davidroberts](#):
>
> Undefined variable $items”

So the error message tells you what the problem is: Your variable $items is not defined.

---

<div class="post-metadata">

**Author:** ![davidroberts](https://avatars.discourse-cdn.com/v4/letter/d/3da27b/32.png) [@davidroberts](https://forum.getkirby.com/u/davidroberts)\
**Post date:** [May 26, 2023, 6:26pm UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/13 "2023-05-26T18:26:17Z")

</div>

So the error message tells you what the problem is: Your variable $items is not defined.

I don’t understand how do you define the variable $items?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 26, 2023, 6:35pm UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/14 "2023-05-26T18:35:06Z")

</div>

Please refer to the recipe where you found the code. It’s all there.

---

<div class="post-metadata">

**Author:** ![davidroberts](https://avatars.discourse-cdn.com/v4/letter/d/3da27b/32.png) [@davidroberts](https://forum.getkirby.com/u/davidroberts)\
**Post date:** [May 29, 2023, 5:23pm UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/15 "2023-05-29T17:23:33Z")

</div>

How do I make the ‘services’ and ‘information’ menu items not clickable? So only the sub-menu items are.

![CleanShot 2023-05-23 at 1.32.09](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/5/5d86bdc37cbf9d91cad4db9cd3752d7f71133bff.jpeg)

---

<div class="post-metadata">

**Author:** ![GB\_DESIGN](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gb_design/32/14956_2.png) [@GB\_DESIGN](https://forum.getkirby.com/u/GB_DESIGN)\
**Post date:** [May 31, 2023, 7:52am UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/17 "2023-05-31T07:52:24Z")

</div>

I am also looking for a solution for this. As shown in your example, “services” and “information” should only open the submenu, but not display a content page. But in my case I use a different treemenu, nevertheless the principle is identical.

---

<div class="post-metadata">

**Author:** ![davidroberts](https://avatars.discourse-cdn.com/v4/letter/d/3da27b/32.png) [@davidroberts](https://forum.getkirby.com/u/davidroberts)\
**Post date:** [May 31, 2023, 8:39am UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/18 "2023-05-31T08:39:06Z")

</div>

Hi,

I don’t have a solution I’m afraid which I need but have not heard back anything from Kirby support and as I am not a backend coder/developer I don’t have an answer. So if you come across how this can work I would be most appreciated.

Thanks,

David

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 31, 2023, 11:06am UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/19 "2023-05-31T11:06:23Z")

</div>

> [@GB\_DESIGN](#):
>
> But in my case I use a different treemenu, nevertheless the principle is identical.

You can use an if statement to decide if a page should be wrapped in an `a` tag or not. So if the page does have children, don’t use a link, but just a button to open the submenu.

---

<div class="post-metadata">

**Author:** ![GB\_DESIGN](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gb_design/32/14956_2.png) [@GB\_DESIGN](https://forum.getkirby.com/u/GB_DESIGN)\
**Post date:** [May 31, 2023, 11:27am UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/20 "2023-05-31T11:27:22Z")

</div>

Thanks for your suggestion. I really like the navigation you use in your guide https://getkirby.com/docs/guide/quickstart. If I click on a menu with submenu, the first article appears directly. Additionally, I have the possibility to expand or collapse the menu with +/-.  
Is there a guide/cookbook how this navigation was realized with Kirby?  
Thank you in advance for your support!

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 31, 2023, 11:30am UTC](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634/21 "2023-05-31T11:30:57Z")

</div>

The repo for the [getkirby.com](http://getkirby.com) website is public and available on GitHub:

> **[GitHub - getkirby/getkirby.com: Source code and content for the Kirby website](https://github.com/getkirby/getkirby.com)**
>
> Source code and content for the Kirby website. Contribute to getkirby/getkirby.com development by creating an account on GitHub.

There’s a lot to learn from there (but not always easy to find your way around, as it is pretty complex).

[Next page](https://forum.getkirby.com/t/nested-menu-sub-menu-items-not-collasping/28634.md?page=2)
