检索数组
4 个回答
- 投票数
-
- 2013-08-21
wp_get_nav_menu_items 似乎正是您所需要的.它返回一个菜单对象数组.
wp_get_nav_menu_items would appear to be exactly what you require. It returns an array of menu objects.
-
- 2016-12-22
正如@vancoder提到的,
wp_get_nav_menu_items()
是必经之路,但是我认为更好的详细答案对以后的人们会有所帮助.所述函数返回一个
WP_Post Object
对象的数组(因此,您可以使用箭头访问值,例如$item->title
).对于基本设置,您可以使用以下命令:
$menuLocations = get_nav_menu_locations(); // Get our nav locations (set in our theme, usually functions.php) // This returns an array of menu locations ([LOCATION_NAME] = MENU_ID); $menuID = $menuLocations['primary']; // Get the *primary* menu ID $primaryNav = wp_get_nav_menu_items($menuID); // Get the array of wp objects, the nav items for our queried location.
然后,您可以循环遍历
$primaryNav
变量,例如:foreach ( $primaryNav as $navItem ) { echo '<li><a href="'.$navItem->url.'" title="'.$navItem->title.'">'.$navItem->title.'</a></li>'; }
As @vancoder mentions,
wp_get_nav_menu_items()
is the way to go, however I think a better detailed answer would be more helpful for people in the future.Said function returns an array of
WP_Post Object
objects (so you access the values with an arrow, eg.$item->title
).For a basic setup, you could use the following:
$menuLocations = get_nav_menu_locations(); // Get our nav locations (set in our theme, usually functions.php) // This returns an array of menu locations ([LOCATION_NAME] = MENU_ID); $menuID = $menuLocations['primary']; // Get the *primary* menu ID $primaryNav = wp_get_nav_menu_items($menuID); // Get the array of wp objects, the nav items for our queried location.
Then you can just loop over that
$primaryNav
variable, for example:foreach ( $primaryNav as $navItem ) { echo '<li><a href="'.$navItem->url.'" title="'.$navItem->title.'">'.$navItem->title.'</a></li>'; }
-
对我来说这返回"假"to me this returns "false"
- 1
- 2018-01-24
- John Smith
-
您是否在复制粘贴@JohnSmith?您需要使用_your_菜单的ID.Are you copy-pasting @JohnSmith? You'll need to use _your_ menu's ID.
- 4
- 2018-02-22
- Brad Adams
-
我发现这比使用walker类要容易得多.好一个!I found this far easier than using the walker class. Nice one!
- 0
- 2019-03-27
- Lee
-
- 2019-07-25
获取简单的菜单数组.
添加到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; ?>
Get simple array of menu.
Add To 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; }
Go to Your Header And Call Function
<?php $a = wp_get_menu_array('top-menu-header'); print_r($a); exit; ?>
-
这是一个不错的答案,但是只有1级深.This is a decent answer, however it only goes 1 level deep.
- 0
- 2019-09-11
- Hybrid Web Dev
-
- 2020-05-21
具有递归循环的上述的修改版本.
添加到Functions.php
function wp_get_menu_array($current_menu='Main Menu') { $menu_array = wp_get_nav_menu_items($current_menu); $menu = array(); function populate_children($menu_array, $menu_item) { $children = array(); if (!empty($menu_array)){ foreach ($menu_array as $k=>$m) { if ($m->menu_item_parent == $menu_item->ID) { $children[$m->ID] = array(); $children[$m->ID]['ID'] = $m->ID; $children[$m->ID]['title'] = $m->title; $children[$m->ID]['url'] = $m->url; unset($menu_array[$k]); $children[$m->ID]['children'] = populate_children($menu_array, $m); } } }; return $children; } foreach ($menu_array 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'] = populate_children($menu_array, $m); } } return $menu; }
Modified version of above with recursive loops.
Add To Functions.php
function wp_get_menu_array($current_menu='Main Menu') { $menu_array = wp_get_nav_menu_items($current_menu); $menu = array(); function populate_children($menu_array, $menu_item) { $children = array(); if (!empty($menu_array)){ foreach ($menu_array as $k=>$m) { if ($m->menu_item_parent == $menu_item->ID) { $children[$m->ID] = array(); $children[$m->ID]['ID'] = $m->ID; $children[$m->ID]['title'] = $m->title; $children[$m->ID]['url'] = $m->url; unset($menu_array[$k]); $children[$m->ID]['children'] = populate_children($menu_array, $m); } } }; return $children; } foreach ($menu_array 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'] = populate_children($menu_array, $m); } } return $menu; }
我目前正在研究自定义主题.在这个主题中,我有一个使用
register-nav-menu
注册的菜单,然后使用wp_nav_menu
函数将其显示在网页上.>但是,我只想检索菜单项列表(仅在wp-admin区域中用于创建菜单的实际菜单项名称,而没有任何html),并且我想保存该菜单项列表到一个数组(即不显示它).
因此,伪代码看起来像这样
有办法吗?谢谢.