How to control which parent menu items are shown?

#1

Hi, I would like to hide some parent menu items. How does it work? Do I have to write something in page blueprint to make it hidden in menu? Best would be to have a checkbox field in panel in that page editing, so that client manage himself.

Also sometimes would need, that page, that is first level, would be displayed as child (2 level) in menu. How is it possible to do?

My current menu structure is:

  <div class="navbar">
    <!-- Navbar Links -->
    <?php foreach ($site->children()->listed() as $p): ?>

    <?php if ($p->menu() == "yes" && ($p->hasvisibleChildren() && $p->template() != 'default')): ?>
    <div class="navbar-item has-dropdown is-hoverable">
      <a class="navbar-link <?php e($p->isOpen(), 'is-active') ?>" href="<?= $p->url() ?>">
      <?= $p->title() ?>
      </a>

      <div class="navbar-dropdown">
      <?php if($p->template() == "blog"): ?>
        <?php foreach($p->children()->listed()->flip()->limit(10) as $n): ?>
        <a class="navbar-item <?= e($n->isOpen(), 'is-active') ?>" href="<?= $n->url() ?>">
          <?= $n->title() ?>
        </a>
        <?php endforeach ?>
      <?php else: ?>
        <?php foreach($p->children()->flip()->listed() as $n): ?>
        <a class="navbar-item <?= e($n->isOpen(), 'is-active') ?>" href="<?= $n->url() ?>">
          <?= $n->title() ?>
        </a>
        <?php endforeach ?>
      <?php endif ?>
      </div>
    </div>

    <?php else: ?>

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

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

  
  
  </div>
#2

I use a toggle field in each page to control menu visibility. Then you can just do this in your loop:

<?php if($sitenav->featureinnav()->bool()): ?>
<!-- menu link -->
<?php endif ?>
#3

I’d probably use a nested structure field to fully control the menu manually (with pages fields inside the structure)

      fields:
        type: fields
        headline: Menu
        fields:
          menu:
            type: structure
            fields:
              mainitem:
                label: Main Menu Item
                type: pages
                query: site.index # you can adapt this query to exclude pages that shouldn't appear in the main menu, like the error page or subpages etc.
              level2:
                type: structure
                fields:
                  subitem:
                    label: Child Menu Item
                    type: pages

The toggle field approach works fine if you just want to include/exclude pages from the navigation. But if you want to control the level where in the navigation an item appears, or the order in which the items appear in the navigation manually, the toggle field approach is not enough.

GUI Nav Builder
#4

Thank you very much, will try both approaches.

texnixe, how do I filter out second level pages from this selection?

Also can you please guide how to display menu? What code should it be?