質問: get_list_of_items($name_of_menu); // $name_of_menu is the result of register-nav-menu, and get_list_of_items is the psuedo function foreach ($menu_items as $item) { // do something with the item } そうする方法はありますか?ありがとう。
回答: メニューの単純な配列を取得します。functions.phpに追加 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]['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; $menu[$m->menu_item_parent]['children'][$m->id] = $submenu[$m->id]; } } return $menu; } ヘッダーに移動して関数を呼び出す<?php $a = wp_get_menu_array('top-menu-header'); print_r($a); exit; ?>