Change order of menu items depending on current active page/item
-
The site I’m working on is divided in four global sections, basically four parent menu items with different sub menus each. The requirement is that the “current” parent menu item with its sub menu should always be at the top (i. e. the first item). So, simply spoken: if I click parent item 2 or one of its children, parent should become parent item 1.
At the same time, each global section has a slightly different style (e. g. different color scheme). So, for instance, whenever a page assigned to parent item 1 or its children is active, the text color should be green and whenever a page assigned to parent item 2 or its children is active, the text color should be blue etc.
I’m going to tell you what I’ve currently done so far but if you think there is a simpler or smarter way to do it I’m certainly open for suggestions.
So, the way I’ve currently set up the whole thing is that I’ve created categories for pages, and for each top-level category a new menu is registered:$main_categories = get_categories( array( 'orderby' => 'name', 'parent' => 0 )); foreach($main_categories as $key => $cat): register_nav_menu('nav_'.$key, 'Menu for '.$cat->name); endforeach;
This way each category gets its own user-editable sub menu. And the template code to output the menu in header.php looks like this at the moment:
<ul> <?php $main_categories = get_categories( array( 'orderby' => 'name', 'parent' => 0, 'hide_empty' => false )); $nav_locations = get_nav_menu_locations(); // retrieve all existing nav menu locations // loop through main categories and create menu if assigned in admin foreach($main_categories as $key => $cat): if(isset($nav_locations['nav_'.$key])): // if array item for menu exists $menu_id = $nav_locations['nav_'.$key]; // ID of the menu assigned in the current iteration $items = wp_get_nav_menu_items($menu_id); // get all menu items of current iteration’s menu ?> <!-- begin parent list item --> <li class="<?= $cat->slug ?>"> <a<?php if($items) { ?> href="<?= $items[0]->url; ?>"<?php } ?>><?= $cat->name ?></a> <span><?= $cat->description ?></span> <?php if($items): // if child items exist ?> <ul> <?php foreach($items as $item): // loop over child menu items if($item->menu_item_parent): // output all child menu items ?> <li><a href="<?= $item->url ?>"><?= $item->title ?></a></li> <?php endif; endforeach; ?> </ul> </li> <?php endif; endif; endforeach; ?> </ul>
This works good so far. But the challenge is now to re-order the category array (parent items) according to the “current” status of the child menu items, i.e. if one of the child items is a “current page” the parent should be first in the category array.
Does that make sense to you? What are the options here? And really, if you think that there is a better approach than my current one, please don’t hesitate to tell me about it. I’d be very grateful for any inspiration you can give me. And let me know if you need any additional info.
- The topic ‘Change order of menu items depending on current active page/item’ is closed to new replies.