I think this should be simple, but the solution is evading me. I’m using the treemenu to drive a main menu, but with a slight tweek. I have a checkbox field on my articles that is a true/false for that page showing in any menus. However, when i try and add this to the treemenu i end up with an empty UL for those pages that are set to false, for obvious reasons.
<?php if(!isset($subpages)) $subpages = $site->pages() ?>
<ul id="">
<?php foreach($subpages->visible() as $p): ?>
<?php if ($p->featureinnav()->bool()): ?>
<li class="depth-<?php echo $p->depth() ?>">
<a<?php echo ($p->isActive()) ? ' class="active"' : '' ?> href="<?php echo $p->url() ?>"><?php echo $p->title() ?></a>
<?php if($p->hasChildren()): ?>
<?php snippet('navs/nav-treemenu', array('subpages' => $p->children())) ?>
<?php endif ?>
</li>
<?php endif ?>
<?php endforeach ?>
</ul>
How do i tweak the above so it still works recursively and ignores the articles set to false, regardless of how deep the page is without empty tags?
Use a filter method:
<?php if(!isset($subpages)) $subpages = $site->pages()->filterBy('featureinnav', '1') ?>
<ul id="">
<?php foreach($subpages->visible() as $p): ?>
<li class="depth-<?php echo $p->depth() ?>">
<a<?php echo ($p->isActive()) ? ' class="active"' : '' ?> href="<?php echo $p->url() ?>"><?php echo $p->title() ?></a>
<?php if($p->hasChildren()): ?>
<?php snippet('navs/nav-treemenu', array('subpages' => $p->children()->filterBy('featureinnav', '1'))) ?>
</li>
<?php endif ?>
<?php endforeach ?>
</ul>
You probably don’t want to filter the first level pages, though, or even turn it round:
<?php if(!isset($subpages)) $subpages = $site->pages()->filterBy('featureinnav', '!=', '0') ?>
<ul id="">
<?php foreach($subpages->visible() as $p): ?>
<li class="depth-<?php echo $p->depth() ?>">
<a<?php echo ($p->isActive()) ? ' class="active"' : '' ?> href="<?php echo $p->url() ?>"><?php echo $p->title() ?></a>
<?php if($p->hasChildren()): ?>
<?php snippet('navs/nav-treemenu', array('subpages' => $p->children()->filterBy('featureinnav', '!=', '0'))) ?>
</li>
<?php endif ?>
<?php endforeach ?>
</ul>
Thanks, but neither of those works. The first one hides everything, the second one shows everything. Just noticed I used the toggle field for the feartureinnav()
instead of the previously stated checkbox… Hmmm…
Ok I switched to true / false because, its a toggle field:
<?php if(!isset($subpages)) $subpages = $site->pages()->filterBy('featureinnav', 'true') ?>
<ul id="">
<?php foreach($subpages->visible() as $p): ?>
<li class="depth-<?php echo $p->depth() ?>">
<a<?php echo ($p->isActive()) ? ' class="active"' : '' ?> href="<?php echo $p->url() ?>"><?php echo $p->title() ?></a>
<?php if($p->hasChildren()): ?>
<?php snippet('navs/nav-treemenu', array('subpages' => $p->children()->filterBy('featureinnav', 'true'))) ?>
</li>
<?php endif ?>
<?php endforeach ?>
</ul>
However, I still get an empty ul
for the ones set to false. Its a tricky one…
texnixe
October 15, 2017, 11:12am
5
First of all, your endif is not located correctly, should be after the snippet call.
<?php if(!isset($subpages)) $subpages = $site->pages()->filterBy('featureinnav', 'true') ?>
<ul id="">
<?php foreach($subpages->visible() as $p): ?>
<li class="depth-<?php echo $p->depth() ?>">
<a<?php echo ($p->isActive()) ? ' class="active"' : '' ?> href="<?php echo $p->url() ?>"><?php echo $p->title() ?></a>
<?php if($p->hasChildren()): ?>
<?php snippet('navs/nav-treemenu', array('subpages' => $p->children()->filterBy('featureinnav', 'true'))) ?>
<?php endif ?>
</li>
<?php endforeach ?>
</ul>
Secondly, you probably have to extend your if statement:
<?php if($p->hasChildren() && $p->children()->filterBy('featureinnav', 'true')->count()): ?>
Thanks! This works…
<?php if(!isset($subpages)) $subpages = $site->pages()->filterBy('featureinnav', 'true') ?>
<ul class="menu-group">
<?php foreach($subpages->visible() as $p): ?>
<li class="depth-<?php echo $p->depth() ?>">
<a<?php echo ($p->isActive()) ? ' class="active"' : '' ?> href="<?php echo $p->url() ?>"><?php echo $p->title() ?></a>
<?php if($p->hasChildren() && $p->children()->filterBy('featureinnav', 'true')->count()): ?>
<?php snippet('navs/nav-treemenu', array('subpages' => $p->children()->filterBy('featureinnav', 'true'))) ?>
<?php endif ?>
</li>
<?php endforeach ?>
</ul>
Out of curiosity, why is there a count()
on the end of the if
?
Shouldnt this be enough?
<?php if($p->hasChildren() && $p->children()->filterBy('featureinnav', 'true')): ?>
texnixe
October 15, 2017, 11:33am
7
No, it’s not enough, because an empty collection is still a collection, and therefore will return true.