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
<?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 ?>
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?
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.
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).
<?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 ?>
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.
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 ?>
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.