每个导航菜单项都存储一个名为nav_menu_item
. 水平位置存储在列中menu_order
垂直位置(层次结构)存储为名为_menu_item_menu_item_parent
持有母公司nav_menu_item
ID.创建WordPress查找的嵌套列表_menu_item_menu_item_parent
在每个项目上,如果有具有该ID的项目,则这些帖子将分组在新的子列表中。这基本上就是课堂上Walker_Nav_Menu
做
为了说明这一点,请使用一个小插件:
<?php
/**
* Plugin Name: Inspect Nav Menu Items
* Plugin URI: http://wordpress.stackexchange.com/q/70872
*/
add_filter( \'wp_nav_menu_args\', \'wpse_70872_inspect_nav_menu_items\' );
function wpse_70872_inspect_nav_menu_items( $dummy = 1 )
{
if ( \'wp_nav_menu_args\' === current_filter() )
{
add_action( \'shutdown\', __FUNCTION__ );
return $dummy;
}
// If we made it so far this function was called on \'shutdown\'.
$nav_items = get_posts(
array (
\'numberposts\' => -1,
\'post_type\' => \'nav_menu_item\'
)
);
$out = array ();
foreach ( $nav_items as $nav_item )
{
$out[] = array (
// general post data
\'post_data\' => $nav_item,
// meta data
\'meta\' => get_post_custom( $nav_item->ID ),
// Name of the menu
\'terms\' => wp_get_object_terms( $nav_item->ID, \'nav_menu\' ),
);
}
print \'<pre>\' . esc_html( var_export( $out, TRUE ) ) . \'</pre>\';
}
结果可能如下所示:
array (
0 =>
array (
\'post_data\' =>
stdClass::__set_state(array(
\'ID\' => 695,
\'post_author\' => \'1\',
\'post_date\' => \'2012-10-29 23:24:13\',
\'post_date_gmt\' => \'2012-10-29 23:24:13\',
\'post_content\' => \' \',
\'post_title\' => \'\',
\'post_excerpt\' => \'\',
\'post_status\' => \'publish\',
\'comment_status\' => \'open\',
\'ping_status\' => \'open\',
\'post_password\' => \'\',
\'post_name\' => \'695\',
\'to_ping\' => \'\',
\'pinged\' => \'\',
\'post_modified\' => \'2012-10-29 23:24:13\',
\'post_modified_gmt\' => \'2012-10-29 23:24:13\',
\'post_content_filtered\' => \'\',
\'post_parent\' => 0,
\'guid\' => \'http://wpse.mu.wp/?p=695\',
\'menu_order\' => 9,
\'post_type\' => \'nav_menu_item\',
\'post_mime_type\' => \'\',
\'comment_count\' => \'0\',
\'filter\' => \'raw\',
)),
\'meta\' =>
array (
\'_menu_item_type\' =>
array (
0 => \'taxonomy\',
),
\'_menu_item_menu_item_parent\' =>
array (
0 => \'693\',
),
\'_menu_item_object_id\' =>
array (
0 => \'12\',
),
\'_menu_item_object\' =>
array (
0 => \'category\',
),
\'_menu_item_target\' =>
array (
0 => \'\',
),
\'_menu_item_classes\' =>
array (
0 => \'a:1:{i:0;s:0:"";}\',
),
\'_menu_item_xfn\' =>
array (
0 => \'\',
),
\'_menu_item_url\' =>
array (
0 => \'\',
),
),
\'terms\' =>
array (
0 =>
stdClass::__set_state(array(
\'term_id\' => \'105\',
\'name\' => \'Test 1\',
\'slug\' => \'test-1\',
\'term_group\' => \'0\',
\'term_taxonomy_id\' => \'106\',
\'taxonomy\' => \'nav_menu\',
\'description\' => \'\',
\'parent\' => \'0\',
\'count\' => \'9\',
)),
),
),
1 =>
array (
\'post_data\' =>
stdClass::__set_state(array(
\'ID\' => 694,
\'post_author\' => \'1\',
\'post_date\' => \'2012-10-29 23:24:12\',
\'post_date_gmt\' => \'2012-10-29 23:24:12\',
\'post_content\' => \' \',
\'post_title\' => \'\',
\'post_excerpt\' => \'\',
\'post_status\' => \'publish\',
\'comment_status\' => \'open\',
\'ping_status\' => \'open\',
\'post_password\' => \'\',
\'post_name\' => \'694\',
\'to_ping\' => \'\',
\'pinged\' => \'\',
\'post_modified\' => \'2012-10-29 23:24:12\',
\'post_modified_gmt\' => \'2012-10-29 23:24:12\',
\'post_content_filtered\' => \'\',
\'post_parent\' => 0,
\'guid\' => \'http://wpse.mu.wp/?p=694\',
\'menu_order\' => 8,
\'post_type\' => \'nav_menu_item\',
\'post_mime_type\' => \'\',
\'comment_count\' => \'0\',
\'filter\' => \'raw\',
)),
\'meta\' =>
array (
\'_menu_item_type\' =>
array (
0 => \'taxonomy\',
),
\'_menu_item_menu_item_parent\' =>
array (
0 => \'693\',
),
\'_menu_item_object_id\' =>
array (
0 => \'11\',
),
\'_menu_item_object\' =>
array (
0 => \'category\',
),
\'_menu_item_target\' =>
array (
0 => \'\',
),
\'_menu_item_classes\' =>
array (
0 => \'a:1:{i:0;s:0:"";}\',
),
\'_menu_item_xfn\' =>
array (
0 => \'\',
),
\'_menu_item_url\' =>
array (
0 => \'\',
),
),
\'terms\' =>
array (
0 =>
stdClass::__set_state(array(
\'term_id\' => \'105\',
\'name\' => \'Test 1\',
\'slug\' => \'test-1\',
\'term_group\' => \'0\',
\'term_taxonomy_id\' => \'106\',
\'taxonomy\' => \'nav_menu\',
\'description\' => \'\',
\'parent\' => \'0\',
\'count\' => \'9\',
)),
),
),
// … and so on. It is a long list.