Recuperando una lista de elementos de menú en una matriz
4 respuestas
- votos
-
- 2013-08-21
wp_get_nav_menu_items parecería serexactamente lo quenecesita.Devuelve unamatriz de objetos demenú.
wp_get_nav_menu_items would appear to be exactly what you require. It returns an array of menu objects.
-
- 2016-12-22
Comomenciona @vancoder,
wp_get_nav_menu_items()
esel camino a seguir,sinembargo,creo que una respuestamejor detallada seríamás útilpara laspersonasen elfuturo.Dichafunción devuelve unamatriz de objetos
WP_Post Object
(por lo que accede a los valores con unaflecha,porejemplo,$item->title
).Para una configuración básica ,puede utilizar lo siguiente:
$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.
Luegopuede recorreresa variable
$primaryNav
,porejemplo: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>'; }
-
¿Estás copiando ypegando a @JohnSmith?Deberá utilizarel ID de sumenú.Are you copy-pasting @JohnSmith? You'll need to use _your_ menu's ID.
- 4
- 2018-02-22
- Brad Adams
-
Encontréestomuchomásfácil que usar la clase de caminante.¡Buenaesa!I found this far easier than using the walker class. Nice one!
- 0
- 2019-03-27
- Lee
-
- 2019-07-25
Obtenga una variedad simple demenú.
Agregar a 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; }
Ir a lafunción deencabezado y llamada
<?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; ?>
-
Estaes una respuesta decente,sinembargo,solotiene unnivel deprofundidad.This is a decent answer, however it only goes 1 level deep.
- 0
- 2019-09-11
- Hybrid Web Dev
-
- 2020-05-21
Versiónmodificada de lo anterior conbucles recursivos.
Agregar a 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; }
Actualmenteestoytrabajandoen untemapersonalizado.Enestetema,tengo unmenú que registré usando
register-nav-menu
,y luegoestoymostrandoelmenúen mipágina web usando lafunciónwp_nav_menu
.Sinembargo,me gusta recuperar solo la lista deelementos delmenú (solo losnombres reales de loselementos delmenú que se usaronen el área wp-adminpara crearelmenú,y sinningún html),yme gustaríaguardaresa lista deelementosa unamatriz (es decir,nomostrarla).
Entonces,un códigopsuedo se vería así
¿Hay algunaforma de hacerlo?Gracias.