Abrufen einer Liste von Menüelementen in einem Array
4 Antworten
- Stimmen
-
- 2013-08-21
wp_get_nav_menu_items scheintgenau das zu sein,was Siebenötigen.Esgibt ein Array von Menüobjekten zurück.
wp_get_nav_menu_items would appear to be exactly what you require. It returns an array of menu objects.
-
- 2016-12-22
Wie @vancodererwähnt,ist
wp_get_nav_menu_items()
der richtige Weg,aberich denke,eine detailliertere Antwort wärefür Menschenin Zukunft hilfreicher.Diese Funktiongibt ein Array von
WP_Post Object
-Objekten zurück (Siegreifen alsomit einem Pfeil auf die Werte zu,z. B.$item->title
).Fürein grundlegendes Setup können Sie Folgendes verwenden:
$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.
Dann können Sieeinfach die Variable
$primaryNav
durchlaufen,zum Beispiel: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>'; }
-
fürmichgibt dies "falsch" zurückto me this returns "false"
- 1
- 2018-01-24
- John Smith
-
Kopieren Sie @JohnSmith?Siemüssen die ID Ihres Menüs verwenden.Are you copy-pasting @JohnSmith? You'll need to use _your_ menu's ID.
- 4
- 2018-02-22
- Brad Adams
-
Ichfand das vieleinfacher als die Walker-Klasse.Schön!I found this far easier than using the walker class. Nice one!
- 0
- 2019-03-27
- Lee
-
- 2019-07-25
Holen Sie sichein einfaches Menü.
Zu Funktionen hinzufügen.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; }
Gehen Sie zu Ihrer Header- und Aufruffunktion
<?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; ?>
-
Diesisteine anständige Antwort,diejedochnur 1 Ebenetief geht.This is a decent answer, however it only goes 1 level deep.
- 0
- 2019-09-11
- Hybrid Web Dev
-
- 2020-05-21
Geänderte Version von obenmit rekursiven Schleifen.
Zu Funktionen hinzufügen.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; }
Ich arbeite derzeit aneinem benutzerdefinierten Thema.In diesem Thema habeichein Menü,dasichmit
register-nav-menu
registriert habe,und dann zeigeich das Menümit der Funktionwp_nav_menu
aufmeiner Webseite an.Ichmöchtejedochnur die Liste der Menüelemente abrufen (nur dietatsächlichen Namen der Menüelemente,dieim wp-admin-Bereich zum Erstellen des Menüs verwendet wurden,und zwar ohne HTML),undichmöchte diese Liste der Elemente speichernzueinem Array (dhnicht anzeigen).
Ein Pseudo-Code würde also so aussehen
Gibteseine Möglichkeit,dies zutun?Danke.