Display all submenus

时间:2013-03-25 作者:SunnyRed

对于我想显示的自定义jquery菜单all submenus 并根据需要隐藏/显示每个。然而,我的问题是首先显示所有子菜单。

截至目前,我正在管理first level 通过admin menu 部分:将菜单项添加到主菜单。显示器的工作原理如下:

wp_nav_menu(
   array (
        \'sort_column\' => \'menu_order\'
        , \'container\' => \'ul\'
        , \'menu_id\' => \'main-nav\'
        , \'theme_location\' => \'primary-menu\'
        , \'depth\' => 1
   )
);
Thesecond level (不幸的)是通过pages section 具有post\\u父项和menu\\u顺序。因此,显示它的方式如下:

global $post;
$has_post_parent = $post && $post->post_parent;
$post_id = $post ? $post->ID : -1;
$top = $has_post_parent ? array_pop( get_post_ancestors($post_id) ) : $post_id;
echo \'<div id="submenu" class="clearfix"><ul>\';
wp_list_pages(
    array(
        \'child_of\' => $top
        , \'depth\' => 1
        , \'title_li\' => \'\'
    )
);
echo \'</ul></div>\';
我已经尝试循环浏览主菜单项(第一级),如下所示,但这使我的子菜单空了,尽管主菜单项乍看起来似乎是正确的:

$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object($locations[\'primary-menu\']);
$menu_items = wp_get_nav_menu_items($menu->term_id);
foreach($menu_items as $menu_item) {
    $current_id = $menu_item->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>\';
}

EDIT

尽管我已经修复了我的初始解决方案,但我仍然对一个解决方案非常感兴趣,其中的菜单可以completely managed through the wp-admin menus section.

1 个回复
最合适的回答,由SO网友:SunnyRed 整理而成

好的,我的解决方案中的问题是$menu_item->ID 而不是
$menu_item->object_id. 因此,完全正确的逻辑如下:

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>\';
}

结束

相关推荐

Custom menus not showing

作为我上一次关于菜单的未解决查询的后续,这个问题已经进一步扩展。我的菜单没有打印代码中的任何地方。我正在注册菜单功能。php:add_action( \'after_setup_theme\', \'your_newtheme_setup\' ); if ( ! function_exists( \'your_newtheme_setup\' ) ) : function your_newtheme_setup() { if (