如何访问到子菜单的说明

时间:2018-01-08 作者:Jordi Vicens

嗯,我正在尝试获取一个菜单项和一个子菜单的描述,并附上下面的代码,但我无法获取它。

我试图得到“关于我们”和“我们董事会员工”的描述:

About us and Our board and staff

因为菜单项(关于我们)运行良好,但由于某种原因,子菜单(我们的董事会和员工)不包含信息description 它只是ID, URLTitle, 我已经试过了var_dump() 子菜单对象(正如您在下面的代码中所看到的),但它没有。

function get_menu_section_description($sectionUrl){     

    $menu = wp_get_menu_array("menu");

    $desc = "";

    foreach ($menu as $key => $item){
        $arr = $item[\'url\'];
        // var_dump($item);
        if ($sectionUrl == $arr[0]) {
            $desc = $item[\'description\'];
        }

        if(sizeof($item[\'children\']) > 0){
            foreach ($item[\'children\'] as $key => $children){
                // var_dump($children);
                $arr2 = $children[\'url\'];
                if ($sectionUrl == $arr2) {
                    $desc = $children[\'description\'];
                }
            }
        }   }   return $desc; }
有人知道为什么没有描述项,如何激活它或可能的解决方案吗?提前谢谢。

1 个回复
SO网友:Jordi Vicens

我几天就找到了一个解决方案,所以希望它能帮助别人,我遇到的问题是调用菜单的功能wp_get_menu_array(), 在那里,我不得不在子菜单中添加描述:

/**
 * Get a simple array of menu mapped, including the submenus
 *
 * @param array $current_menu Argument where you send the slug of the menu that you want to map.
 * @return array returns a simple array with the information.
 */
function wp_get_menu_array($current_menu) {

    $array_menu = wp_get_nav_menu_items($current_menu);
    $menu = array();
    foreach ($array_menu as $m) {

        if (empty($m->menu_item_parent)) {
            $menu[$m->ID] = array();
            $menu[$m->ID][\'ID\']      =   $m->ID;
            $menu[$m->ID][\'title\']       =   $m->title;
            $menu[$m->ID][\'url\']         =   $m->url;
            $menu[$m->ID][\'classes\']     =   $m->classes;
            $menu[$m->ID][\'description\']     =   $m->description;
            $menu[$m->ID][\'children\']    =   array();
        }
    }
    $submenu = array();
    foreach ($array_menu as $m) {
        if ($m->menu_item_parent) {
            $submenu[$m->ID] = array();
            $submenu[$m->ID][\'ID\']       =   $m->ID;
            $submenu[$m->ID][\'title\']    =   $m->title;
            $submenu[$m->ID][\'url\']  =   $m->url;
            $submenu[$m->ID][\'description\']  =   $m->description; //Line added;
            $menu[$m->menu_item_parent][\'children\'][$m->ID] = $submenu[$m->ID];
        }
    }
    return $menu;

}

结束