-
-
Save josecarlospsh/95ca08dae2bbb0ad1067 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function my_custom_submenu() { | |
global $post; | |
$menu_items = wp_get_nav_menu_items('Menu'); | |
$current_menu_id = 0; | |
// get current top level menu item id | |
foreach ( $menu_items as $item ) { | |
if ( $item->object_id == $post->ID ) { | |
// if it's a top level page, set the current id as this page. if it's a subpage, set the current id as the parent | |
$current_menu_id = ( $item->menu_item_parent ) ? $item->menu_item_parent : $item->ID; | |
break; | |
} | |
} | |
// display the submenu | |
echo "<ul id='supplementary_menu'>"; | |
foreach ( $menu_items as $item ) { | |
if ( $item->menu_item_parent == $current_menu_id ) { | |
$class = ( $item->object_id == $post->ID ) ? "class='current_page_item'" : ""; | |
echo "<li {$class}><a href='{$item->url}'>{$item->title}</a>"; | |
$sub_menu_items = []; | |
foreach ( $menu_items as $sub_item ) { | |
if ( $sub_item->menu_item_parent == $item->ID ) | |
$sub_menu_items[] = $sub_item; | |
} | |
if ( $sub_menu_items ) { | |
echo "<ul>"; | |
foreach ( $sub_menu_items as $sub_item ) { | |
$class = ( $sub_item->object_id == $post->ID ) ? "class='current_page_item'" : ""; | |
echo "<li {$class}><a href='{$sub_item->url}'>{$sub_item->title}</a>"; | |
} | |
echo "</ul>"; | |
} | |
echo "</li>"; | |
} | |
} | |
echo "</ul>"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment