wordpress获取数组中的所有顶级菜单

时间:2014-04-01 05:41:21

标签: arrays wordpress menu

我想从wordpress的管理面板中获取菜单部分中设置的数组中的所有顶级菜单。

请帮忙。

由于

2 个答案:

答案 0 :(得分:0)

在你的主题function.php中:

register_nav_menus( array(
    'top_level_new' => __( 'Mail Navigation', 'twentyten' ),
) );

上述代码块允许您创建单独级别的菜单。

只需复制并粘贴代码即可创建单独级别的菜单。

转到wp-admin面板。

从菜单选择器中选择菜单,将页面添加到菜单中。

以下是允许您获取前端菜单的代码块。

<?php

$defaults = array(
    'theme_location'  => 'top_level_new',
    'menu'            => '',
    'container'       => 'div',
    'container_class' => '',
    'container_id'    => '',
    'menu_class'      => 'menu',
    'menu_id'         => '',
    'echo'            => true,
    'fallback_cb'     => 'wp_page_menu',
    'before'          => '',
    'after'           => '',
    'link_before'     => '',
    'link_after'      => '',
    'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
    'depth'           => 0,
    'walker'          => ''
);

wp_nav_menu( $defaults );

?>

答案 1 :(得分:0)

<?php
function wp_get_menu_array($current_menu) {

    $array_menu = wp_get_nav_menu_items($current_menu);
    $menu = array();
    foreach ($array_menu as $m1) {
        if (empty($m1->menu_item_parent)) {
            $menu[$m1->ID] = array();
            $menu[$m1->ID]['ID']             =   $m1->ID;
            $menu[$m1->ID]['title']          =   $m1->title;
            $menu[$m1->ID]['url']            =   $m1->url;
            $menu[$m1->ID]['children']       =   array();
            $childMenu = array();
            foreach ($array_menu as $m2) {
                if ($m2->menu_item_parent == $m1->ID) {
                    $childMenu[$m2->ID] = array();
                    $childMenu[$m2->ID]['ID']          =   $m2->ID;
                    $childMenu[$m2->ID]['title']       =   $m2->title;
                    $childMenu[$m2->ID]['url']         =   $m2->url;
                    $childMenu[$m2->ID]['children']       =   array();
                    $grandChildMenu = array();
                    foreach ($array_menu as $m3) {
                        if ($m3->menu_item_parent == $m2->ID) {
                            $grandChildMenu[$m3->ID] = array();
                            $grandChildMenu[$m3->ID]['ID']          =   $m3->ID;
                            $grandChildMenu[$m3->ID]['title']       =   $m3->title;
                            $grandChildMenu[$m3->ID]['url']         =   $m3->url;
                            $childMenu[$m3->menu_item_parent]['children'][$m3->ID] = $grandChildMenu[$m3->ID];
                        }
                    }
                    $menu[$m2->menu_item_parent]['children'][$m2->ID] = $childMenu[$m2->ID];
                }
            }
        }
    }
    return $menu;
}
?>