-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp_customSubMenu.php
41 lines (35 loc) · 1.33 KB
/
wp_customSubMenu.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php $menuItems = wp_get_nav_menu_items('New Main Navigation'); // Add your menu name here.
if ($menuItems && isset($post->ID) ):
// loop through menu items
foreach($menuItems as $menuItem) :
// if the menu item ID matches the current post ID = current page
if($menuItem->object_id == $post->ID){
//if the current menu item has a parent = sub level nav item
if($menuItem->menu_item_parent != 0){
//set the variable to the parent menu item ID so we can display siblings
$childrenof = $menuItem->menu_item_parent;
} else {
//set the variable to the current list item ID so we can find the children
$childrenof = $menuItem->db_id;
}
}
endforeach;
?>
<?php if(isset($childrenof)): // if this page is in the menu?>
<ul>
<?php foreach($menuItems as $menuItem): // loop through the menu?>
<?php if($menuItem->menu_item_parent == $childrenof): // if the item has the same parent as it's sibling.. OR if it is a sub page of a top level item ?>
<?php if($menuItem->object_id == $post->ID): // if it is the current page ?>
<li class="current-menu-item">
<?php else: ?>
<li>
<?php endif; ?>
<a href="<?php echo $menuItem->url ; ?>">
<?php echo $menuItem->title; ?>
</a>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php endif; ?>