如何处理自定义菜单层次结构输出?

时间:2012-10-29 作者:Jarrod

我希望通过RSS提要传递自定义nav\\u菜单,以便我可以在其他站点上获取它们,从而在多个站点上创建相同的自定义菜单。

通过查看数据库,我无法弄清楚WP是如何存储数据的,它需要知道哪些项与层次结构的父项>子项相关。它如何知道将“子”页面作为单独的ul输出,即使它们没有“正式”设置为父级>子级(从edit post屏幕),仅在菜单区域中可视?

我可以查看数据库中nav\\U menu\\u项目的post类型,menu\\u order列按从上到下的顺序列出项目,但似乎没有任何内容引用menu>子菜单关系。

谢谢你能告诉我的一切。

1 个回复
最合适的回答,由SO网友:fuxia 整理而成

每个导航菜单项都存储一个名为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.

结束