Wie kann man var_dump nav-Menüelemente von überall aus verwenden?
1 Antworten
- Stimmen
Die Elemente werdenin wp_nav_menu()
eingerichtet. Esgibt einennützlichen Filter,den Sie verwenden können: 'wp_nav_menu_objects'
. Esbietet die Elemente als $sorted_menu_items
und die Argumente des Aufrufs wp_nav_menu()
als $args
an.
Von wp-includes/nav-menu-template.php::wp_nav_menu()
:
$sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args );
Also ...
$sorted_menu_items
unverändert und Inmeinem folgenden Beispiel druckeich die Daten auf 'shutdown'
- dasist derneueste Hook,den WordPress auslöst.
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: T5 Collect Nav Menu Items
* Description: Appends debug information about currently processed nav menu items to pages.
*/
add_filter( 'wp_nav_menu_objects', array ( 'T5_Nav_Menu_Collector', 'collect' ), 99, 2 );
class T5_Nav_Menu_Collector
{
/**
* Stores the data
* @type array
*/
protected static $collection = array ();
/**
* Collect nav menu data.
*
* @wp-hook wp_nav_menu_objects
* @param array $sorted_menu_items
* @param array $args
* @return array $sorted_menu_items Not changed.
*/
public static function collect( $sorted_menu_items, $args )
{
// Since we *know* we have data, we register the print action.
add_action( 'shutdown', array ( __CLASS__, 'print_collection' ) );
self::$collection[] = array(
'args' => $args,
'items' => $sorted_menu_items
);
return $sorted_menu_items;
}
/**
* Dump the collected data.
*
* @wp-hook shutdown
* @return void
*/
public static function print_collection()
{
$output = htmlspecialchars( print_r( self::$collection, TRUE ) );
print "<pre><b>Nav Menu Data</b>\n\n$output</pre>";
}
}
Wennes kein Navigationsmenügibt,passiertnichts. Wenn wirjedocheinige Daten sammeln könnten,erhalten wir am Endeeines Dokumentseine schöne lange Listemit allen Elementeigenschaften,die wirin anderen Filtern verwenden können.
The items are set up in wp_nav_menu()
. There is a useful filter you can use: 'wp_nav_menu_objects'
. It offers the items as $sorted_menu_items
and the arguments of the wp_nav_menu()
call as $args
.
From wp-includes/nav-menu-template.php::wp_nav_menu()
:
$sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args );
So …
$sorted_menu_items
unchanged and In my following example I print the data on 'shutdown'
– that’s the latest hook WordPress fires.
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: T5 Collect Nav Menu Items
* Description: Appends debug information about currently processed nav menu items to pages.
*/
add_filter( 'wp_nav_menu_objects', array ( 'T5_Nav_Menu_Collector', 'collect' ), 99, 2 );
class T5_Nav_Menu_Collector
{
/**
* Stores the data
* @type array
*/
protected static $collection = array ();
/**
* Collect nav menu data.
*
* @wp-hook wp_nav_menu_objects
* @param array $sorted_menu_items
* @param array $args
* @return array $sorted_menu_items Not changed.
*/
public static function collect( $sorted_menu_items, $args )
{
// Since we *know* we have data, we register the print action.
add_action( 'shutdown', array ( __CLASS__, 'print_collection' ) );
self::$collection[] = array(
'args' => $args,
'items' => $sorted_menu_items
);
return $sorted_menu_items;
}
/**
* Dump the collected data.
*
* @wp-hook shutdown
* @return void
*/
public static function print_collection()
{
$output = htmlspecialchars( print_r( self::$collection, TRUE ) );
print "<pre><b>Nav Menu Data</b>\n\n$output</pre>";
}
}
If there is no nav menu – nothing happens. But if we could collect some data, we get a nice long list at the end of a document with all item properties we can use in other filters.
Ichmöchteeine Vorschau aller Felder anzeigen,die das $item-Array des Navigationsmenüsenthält.Gibteseine Funktion,umes vonirgendwoher abzurufen undin var_dump zuplatzieren?