Come var_dump le voci del menu di navigazione da qualsiasi luogo?
1 risposta
- voti
Glielementi vengonoimpostatiin wp_nav_menu()
. C'è unfiltro utile chepuoi usare: 'wp_nav_menu_objects'
. Offreglielementi come $sorted_menu_items
e gli argomenti della chiamata wp_nav_menu()
come $args
.
Da wp-includes/nav-menu-template.php::wp_nav_menu()
:
$sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args );
Quindi ...
$sorted_menu_items
invariatoe Nelmioesempio seguente stampoi dati su 'shutdown'
,che è l'ultimo hook attivato da WordPress.
<?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>";
}
}
Senon èpresenteilmenu dinavigazione,non accadenulla. Ma sepotessimo raccogliere alcuni dati,otterremmo unbel lungoelenco allafine di un documento contutte leproprietà deglielementi chepossiamo usarein altrifiltri.
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.
Voglio visualizzarein anteprimatuttii campi contenutinell'array $item delmenu dinavigazione.Esiste unafunzioneper recuperarlo da qualsiasi luogoe inserirloin var_dump?