Skip to content

Instantly share code, notes, and snippets.

@josecarlospsh
Forked from levymetal/functions.php
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josecarlospsh/95ca08dae2bbb0ad1067 to your computer and use it in GitHub Desktop.
Save josecarlospsh/95ca08dae2bbb0ad1067 to your computer and use it in GitHub Desktop.
<?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