Recuperando uma lista de itens de menu em uma matriz
4 respostas
- votos
-
- 2013-08-21
wp_get_nav_menu_items pareceriaexatamente o que vocêprecisa.Ele retorna umamatriz de objetos demenu.
wp_get_nav_menu_items would appear to be exactly what you require. It returns an array of menu objects.
-
- 2016-12-22
comomencions @vancoder,
wp_get_nav_menu_items()
é o caminho apercorrer,noentanto,acho que uma respostamelhor detalhada seriamais útilpara aspessoasnofuturo.ditofunção retorna umamatriz de objetos
WP_Post Object
(então você acessa os valores com uma seta,porexemplo.$item->title
).Para uma configuraçãobásica ,vocêpode usar o seguinte:
$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.
Então vocêpode simplesmente loop sobreisso
$primaryNav
variável,porexemplo: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>'; }
-
Paramim,isso retorna "falso"to me this returns "false"
- 1
- 2018-01-24
- John Smith
-
Vocêestá colando @johnsmith?Vocêprecisará usar o ID domenu _our_.Are you copy-pasting @JohnSmith? You'll need to use _your_ menu's ID.
- 4
- 2018-02-22
- Brad Adams
-
Eu acheimuitomaisfácil do que usar a classe Walker.Agradável!I found this far easier than using the walker class. Nice one!
- 0
- 2019-03-27
- Lee
-
- 2019-07-25
Obtenha amatriz simples domenu.
Adicionar àsfunções.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; }
vápara o cabeçalhoe afunção de chamada
<?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; ?>
-
Esta é uma resposta decente,porém só vai 1nívelprofundo.This is a decent answer, however it only goes 1 level deep.
- 0
- 2019-09-11
- Hybrid Web Dev
-
- 2020-05-21
Versãomodificada do acima com loops recursivos.
Adicionar àsfunções.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; }
Atualmenteestoutrabalhandoem umtemapersonalizado.Nestetema,eutenho ummenu que registrei usando Registre-Nav-Menu e,em seguida,euestouexibindo omenunaminhapágina da Web usando o
.wp_nav_menu
função.Noentanto,eugosto apenas de recuperar a lista deitens demenu (osnomes doitem demenu real apenas queforam usadosna área WP-Adminpara criar omenue sem qualquer HTML),e eugostaria de salvaressa lista deitenspara umamatriz (ou seja,nãoexibi-lo).
Então,um código PSueo seria assim
Existe umamaneira defazê-lo?Obrigado.