0

Well, I'm trying to get the description of a menu item and of a sub menu with the code attached below, but I'm not able to get it.

I'm trying to get the description of "About us" and the description of "Our board staff":

About us and Our board and staff

For the menu item (About us) is working good but for some reason the sub menu (Our board and staff) doesn't contain the information description and it just have ID, URL and Title, I already tried a var_dump() of the sub menu object (as you can see it in the code below) but it doesn't has it.

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

Anyone know why doesn't have the description item, how to activate it or a possible solution for that? Thanks in advance.

0

I found a solution few days a go, so may it help someone, the problem I had was was the function to call the menu wp_get_menu_array(), there I had to add the description in the sub menu:

/**
 * 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;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.