Skip to content

Instantly share code, notes, and snippets.

@yoclove
Created November 24, 2017 09:17
Show Gist options
  • Select an option

  • Save yoclove/bcdffd66c5a4183bbdafd99dcd54e910 to your computer and use it in GitHub Desktop.

Select an option

Save yoclove/bcdffd66c5a4183bbdafd99dcd54e910 to your computer and use it in GitHub Desktop.
二级菜单
function print_sub_menus() {
    global $post;

    // get current page
    $post_id = $post ? $post->ID : -1;
    $has_post_parent = $post && $post->post_parent;
    $top = $has_post_parent ? array_pop( get_post_ancestors($post_id) ) : $post_id;

    // get all main menu items
    $locations = get_nav_menu_locations();
    $menu = wp_get_nav_menu_object($locations['primary-menu']);
    $menu_items = wp_get_nav_menu_items($menu->term_id);

    echo '<div id="submenu" class="clearfix">';
    foreach($menu_items as $menu_item) {
        $current_id = $menu_item->object_id;
        $is_current_class = ($current_id == $top) ? 'current-submenu' : '';

        echo "<ul class='$is_current_class' data-parent='$current_id'>";
        wp_list_pages(
            array(
                'child_of' => $current_id
                , 'depth' => 1
                , 'echo' => 1
                , 'title_li' => ''
            )
        );
        echo '</ul>';
    }
    echo '</div>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment