SOLVED - Two navigation menus: using radiobutton's to control which menu items appear in which navigation

Hey! I have two navigation menus in my site and a host of pages. Some of those pages I’d like to be displayed in one menu, others I’d like to be displayed in another, and some pages I’d like to be displayed in both.

My original idea is to use radiomenus to control what appears where, doing something like this.

But I’m not familiar enough with how the radiomenus work to create any kind of if statement. (if 'menu control' == topnav { display in the top navigation } — for example.

Maybe this isn’t the best way of doing this and you can suggest a more robust way? I admit it feels really hacky doing it this way.

Looking forward to hearing your thoughts on this

Cheers
Chrish

By taking the simple example from Kriby’s docs:

<?php

// main menu items
$items = $pages->visible();

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

?>
<nav>
  <ul>
    <?php foreach($items as $item): ?>
    <li><a<?php e($item->isOpen(), ' class="active"') ?> href="<?php echo $item->url() ?>"><?php echo $item->title()->html() ?></a></li>
    <?php endforeach ?>
  </ul>
</nav>
<?php endif ?>

See: http://getkirby.com/docs/solutions/menus

But instead $items = $pages->visible(); you could do $items = $pages->filterBy('menucontrol','topnav')->visible(); - where menucontrol is the name of your blueprint radiobutton field and topnav would be the value chosen to be displayed in that menu.

2 Likes

Working great, thanks man. How would you suggest utilising the ‘both’ option?

This…

<?php $items = $pages->filterby('menucontrol', 'topnav')->visible(); ?>

…would need to become something like…

<?php $items = $pages->filterby('menucontrol', 'topnav', '|', 'both')->visible(); ?>

But I’m not sure how to do an OR statement in the filterBy()? Is it even possible?

1 Like

You could use

->filterBy('menucontrol', '*=', 'nav');

The *= gets all values, which contain the given string, in this case nav. So just make sure that all your menucontrol values that should be included in this both-menu contain nav and everything doesn’t

Trying to wrap my head around this but I’m confused. Using this code I can only get it to display on both, or none at all. Here’s my blueprint:

fields:
  title:
    label: Title
    type:  text
  menucontrol:
    label: Menu Control*
    type: radio
    default: none
    width: 1/3
    options:
      topnav: Topnav
      sidenav: Sidenav
      both: Both
      none: None

The problem is that filtering does not seem to work with two search words. And the filter above will always give you both which start with nav, so you will get topnav and sidenav, which is not what you want to achieve. Maybe you can name them different:

topnav
sidenav
topside

then filterBy('menu control', '*=', 'top') Would output topnav and topside, filterBy('menu control', '*=', 'side') would pull sidenav and topside

2 Likes

That’s got it! Thanks man.