Filtering pages on menu


#1

Hi,

I have this menu:

<?php foreach($pages->visible() as $item): ?>

    <li class="menu-item<?= r($item->isOpen(), ' is-active'),
    r($item->filterBy('template', '!=', 'blog')->hasChildren(), ' menu-item-has-children') ?>">
        <a href="<?= $item->url() ?>"><?= $item->title()->html() ?></a>

        <?php
            $children = $item->children()->visible()->filterBy('template', '!=', 'article');
            if($children->count() > 0):
        ?>

        <ul class="sub-menu">
            <?php foreach($item->children()->visible() as $child): ?>
                <li class="menu-item<?= r($item->isOpen(), ' is-active') ?>">
                    <a href="<?= $child->url() ?>"><?= $child->title()->html() ?>
                    </a>
                </li>
            <?php endforeach ?>
        </ul>
        <?php endif ?>
    </li>

<?php endforeach ?>

It works fine, except for the filter that add the class ‘menu-item-has-children’ to all visible pages that have children except for template ‘blog’.

Any suggestion of how can I exclude that template?


#2

You can only filter a collection not a single page, use two conditions:

if($item->hasChildren() && $item->template() != 'blog') {
// do sth.
}

#3

It works perfect.

Thanks