Custom Dynamic Menu

I am having trouble coming up with some logic. I have a site that has three color coded sections, and a bunch of ancillary pages outsides of those sections.

The sites main menu needs to change according to the section currently being viewed, and another menu displayed if the page is either the home page or some other ancillary page (about, contact, terms and conditions etc).

I have a snippet that can feed page ids into list out the required pages (with help from autoID) but i can seem to figure out the detection of the three sections, any page inside those sections, and else statement for all other pages.

I have tried the following…

<?php $thispageid = $page->autoid()->value() ?>

<?php if($page->isDescendantOf('sectiona') && $thispageid == '65'): ?>
  <!-- SECTION A -->
  <?php snippet('navs/nav-pagelist', array('pagelist' => ['65', '58', '29' ])) ?>
  <?php else: ?>
  <?php snippet('navs/nav-pagelist', array('pagelist' => ['86', '22', '13' ])) ?>
<?php endif ?>

But when i do this multiple times, i get the menu 3 times… sigh

Whats the best way to do this?

Could you provide some more information? So the section are folders that include subfolders, right? Something like

  - home
  - section-a/
    -- subsection-a
    -- subsection-b
  - section-b/
    -- subsection-a
  - section-c
  - contact
  - etc.
  • homepage and the additional pages like contact etc. all get. the same menu
  • section-a, b and c (and their descendants) get an individual menu with different page ids

What is this check for id == 65 doing, is that a child of section-a?

Ok so ill try and break the problem down and maybe forget my attempt above. The page with id of 65 is the top level page for that section in the site root. that was me trying to get the menu to work on the parent page and any descendent of it. 65 id is the page with a uid of sectiona.

The site essentially has 4 different main menus. A unique menu for each of the 3 sections. The pages in the unique menus are controlled by that snippet above with the array of specific IDs.

Any page in section 1 including the top level parent page gets menu 1
Any page in section 2 including the top level parent page gets menu 2
Any page in section 3 including the top level parent page gets menu 3
Any page that doesn’t fit into the above gets menu 4 (Home page, contact page, error page etc) - these pages are not necessarily top level. The about page is top level but has a few child pages, and some grandchildren… they should all get menu 4, regardless of depth.

Make sense?

You can give each page a field, that stores the number of the menu, its belongs to.
Then in the template, you know which menu you want to show.

I don’t really want to go there … this is an existing site I am updating and has close to 200 pages in it. In the time it takes me to add all the fields to the pages i could have figured out the logic that didnt need it…

Any other ideas?

Your condition then didn’t really make sense, because a page cannot at the same time be the section and be. a descendant of it

<?php $thispageid = $page->autoid()->value() ?>

<?php if($page->isDescendantOf('sectiona') || $thispageid == '65'): ?>
<!-- menu 1-->
  <?php snippet('navs/nav-pagelist', array('pagelist' => ['65', '58', '29' ])) ?>
<?php elseif($page->isDescendantOf('section-b') || $thispageid == 'section-b-id'): ?>
<!-- menu 2-->
<?php snippet('navs/nav-pagelist', array('pagelist' => ['4', '5', '6' ])) ?>
<?php elseif($page->isDescendantOf('section-c') || $thispageid == 'section-c-id'): ?>
<!-- menu 3-->
<?php snippet('navs/nav-pagelist', array('pagelist' => ['1', '2', '3' ])) ?>
  <?php else: ?>
<!-- menu 4 -->
  <?php snippet('navs/nav-pagelist', array('pagelist' => ['86', '22', '13' ])) ?>
<?php endif ?>

Ah ha! Thanks, that did the trick, and yeah I started out using or instead of and but was messing around, forget to change it back before i pasted.

Thanks :slight_smile: