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
  • Select an option

  • Save josecarlospsh/95ca08dae2bbb0ad1067 to your computer and use it in GitHub Desktop.

Select an option

Save josecarlospsh/95ca08dae2bbb0ad1067 to your computer and use it in GitHub Desktop.

Revisions

  1. levymetal revised this gist Apr 12, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    <?php

    function my_custom_submenu() {
    global $post;

  2. levymetal created this gist Apr 12, 2013.
    47 changes: 47 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    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>";
    }