# How to show a specfic sub menu only in navigation

**URL:** <https://forum.getkirby.com/t/how-to-show-a-specfic-sub-menu-only-in-navigation/25093>\
**Category:** Questions\
**Tags:** v3\
**Created:** [March 27, 2022, 1:34pm UTC](https://forum.getkirby.com/t/how-to-show-a-specfic-sub-menu-only-in-navigation/25093 "2022-03-27T13:34:12Z")\
**Posts on this page:** 10\
**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:** [March 27, 2022, 1:34pm UTC](https://forum.getkirby.com/t/how-to-show-a-specfic-sub-menu-only-in-navigation/25093/1 "2022-03-27T13:34:12Z")

</div>

Hi  
I want to show a single submenu with ‘children’. I am using the code below which works but shows all ‘children’ in the menu. How do I change the code to just show a specific submenu?  
So below just show Item 1/sub1

e.g. Item 1 item 2 item 3 etc  
sub 1 sub 3  
sub 1 sub 3  
sub 1

```auto
<?php

// nested menu
$items = $pages->listed();

// only show the menu if items are available
if($items->isNotEmpty()):

?>
<nav>
  <ul>
    <?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>
        <?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>
<?php endif ?>

```

---

<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:** [March 27, 2022, 2:15pm UTC](https://forum.getkirby.com/t/how-to-show-a-specfic-sub-menu-only-in-navigation/25093/2 "2022-03-27T14:15:24Z")

</div>

I’m not sure I understand what you are trying to achieve. Do you mean when a certain page is open, you only want to show the children of the open page in the menu?

---

<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:** [March 27, 2022, 5:19pm UTC](https://forum.getkirby.com/t/how-to-show-a-specfic-sub-menu-only-in-navigation/25093/3 "2022-03-27T17:19:14Z")

</div>

I want to be able to show the ‘walks’ dropdown menu (second image) and not have any other dropdown as it appears at the moment under ‘news’. Hope that makes sense.

 ![news](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/2/2412cf2dfbd1fbb3bb5132f2ff41c6a442586a30.jpeg)  
 ![walks](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/d/d994715e4a2fcc2a39b2a39041e7c957a63f076b.jpeg)

---

<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:** [March 27, 2022, 5:29pm UTC](https://forum.getkirby.com/t/how-to-show-a-specfic-sub-menu-only-in-navigation/25093/4 "2022-03-27T17:29:52Z")

</div>

You can use a condition to only show children e.g. based on the intended template (assuming it is called `walks` here, otherwise change as required).

```auto
<?php
// nested menu
$items = $pages->listed();

// only show the menu if items are available
if ($items->isNotEmpty()) :

?>
  <nav>
    <ul>
      <?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
            if ($item->intendedTemplate()->name() === 'walks') : ?>
              <?php
              $children = $item->children()->listed();

              // display the submenu if children are available
              if ($children->isNotEmpty()) :
              ?>
                <ul>
                  <?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 ?>
            <?php endif; ?>
        </li>
      <?php endforeach ?>
    </ul>
  </nav>
<?php endif ?>

```

---

<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:** [March 28, 2022, 4:52pm UTC](https://forum.getkirby.com/t/how-to-show-a-specfic-sub-menu-only-in-navigation/25093/5 "2022-03-28T16:52:26Z")

</div>

texnixe,

Thanks for that code snippet but it does not work. I have a separate folder for each walk in the content folder so I have a ‘walks’ folder and in that folder, I have 1\_tarbert, 2\_claonaig etc. In the templates, I have a file for each walk tarbert, claonaig etc. Do I need to set this up differently to get it to work? Thanks.

---

<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:** [March 28, 2022, 5:00pm UTC](https://forum.getkirby.com/t/how-to-show-a-specfic-sub-menu-only-in-navigation/25093/6 "2022-03-28T17:00:59Z")

</div>

I’m afraid I don’t understand.

> [@davidroberts](#):
>
> In the templates, I have a file for each walk tarbert, claonaig etc.

What does that mean?

And could you please post your folder structure?

> [@davidroberts](#):
>
> but it does not work.

It does not work at all or it does not give you the expected result? What does it give you as opposed to your expected result?

---

<div class="post-metadata">

**Author:** ![SCC](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/scc/32/4116_2.png) [@SCC](https://forum.getkirby.com/u/SCC)\
**Post date:** [March 28, 2022, 7:49pm UTC](https://forum.getkirby.com/t/how-to-show-a-specfic-sub-menu-only-in-navigation/25093/7 "2022-03-28T19:49:30Z")

</div>

Not sure why @texnixe solution does not work, so really need to see the folder structure, the only place I can think this is going wrong.

I have a slightly different solution on my site which was:

```
// Get nav items
<?php $nav_items = $pages->listed(); ?>

// Loop through each nav item    
<?php foreach ($nav_items as $nav_item): ?>

   // If the nav item has children that is not a page called blog or guides
   <?php if ($nav_item->hasListedChildren() && !$nav_item->is(page('blog')) && !$nav_item->is(page('guides'))): ?>

       // Display the nav item name and dropdown links for the children

   <?php else: ?>

        // If the nav item has no listed children or is blog or guides, just display the title link

        <?php endif ?>
        
<?php endforeach ?>

```

it works, not saying it is the cleanest 🙂

---

<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:** [March 29, 2022, 4:18pm UTC](https://forum.getkirby.com/t/how-to-show-a-specfic-sub-menu-only-in-navigation/25093/8 "2022-03-29T16:18:30Z")

</div>

Hi texnixe,

It does not work at all or it does not give you the expected result? What does it give you as opposed to your expected result?

It shows the navigation but the children do not show under ‘walks’.

 ![CleanShot 2022-03-29 at 5.11.37](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/e/e8d3ea9a9c30a9ef5a676a974e0eeb39ee6e8d38.jpeg)  
 ![CleanShot 2022-03-29 at 5.12.05](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/5/589c7ef6061f7d1ab9d610fae050133318550b3d.jpeg)

---

<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:** [March 29, 2022, 4:31pm UTC](https://forum.getkirby.com/t/how-to-show-a-specfic-sub-menu-only-in-navigation/25093/9 "2022-03-29T16:31:57Z")

</div>

> [@texnixe](#):
>
> ` if ($item->intendedTemplate()->name() === 'walks') : ?>`

As I wrote above, I was assuming that your content file (intendedTemplate) was called `walks`. But in reality, it is called `guide`, so have have to replace `walks` in this if statement with `guide`.

---

<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:** [March 31, 2022, 8:01pm UTC](https://forum.getkirby.com/t/how-to-show-a-specfic-sub-menu-only-in-navigation/25093/10 "2022-03-31T20:01:46Z")

</div>

Many thanks for sorting that issue out.
