当前浏览:首页 / WordPress函数 / walk_nav_menu_tree()

walk_nav_menu_tree( array $items, int $depth, stdClass $args )

检索导航菜单项的HTML列表内容

menumore...

nav


参数

$items

(array)(必填) 菜单项,按每个菜单项的菜单顺序排序。

$depth

(int)(必填) 项目相对于父项的深度。

$args

(stdClass)(必填) 包含wp_nav_menu()参数的对象。


返回

(string) 菜单项的HTML列表内容。



源码

查看源码 官方文档


更新日志

版本描述
3.0.0开始引入

使用示例

  • 示例1

    在WordPress导航菜单中添加一个metabox,并添加两个预定义链接(登录、注销)。

    add_action( 'admin_head-nav-menus.php', function() {
        add_meta_box( 'plugin-slug-menu-metabox', "Wordpress Login/Logout", 'wpdocs_plugin_slug_render_menu_metabox', 'nav-menus', 'side', 'default', array( /*custom params*/ ) );
    } );
    
    function wpdocs_plugin_slug_render_menu_metabox( $object, $args )
    {
      global $nav_menu_selected_id;
      // Create an array of objects that imitate Post objects
      $my_items = array(
        (object) array(
            'ID' => 1,
            'object_id' => 1,
            'type_label' => 'Login',
            'title' => 'Login',
            'url' => wp_login_url(),
            'type' => 'custom',
            'object' => 'plugin-slug-slug',
            'db_id' => 0,
            'menu_item_parent' => 0,
            'post_parent' => 0,
            'target' => '',
            'attr_title' => '',
            'description' => '',
            'classes' => array(),
            'xfn' => '',
        ),
        (object) array(
            'ID' => 1,
            'object_id' => 1,
            'type_label' => 'Logout',
            'title' => 'Logout',
            'url' => wp_logout_url(),
            'type' => 'custom',
            'object' => 'plugin-slug-slug',
            'db_id' => 0,
            'menu_item_parent' => 0,
            'post_parent' => 0,
            'target' => '',
            'attr_title' => '',
            'description' => '',
            'classes' => array(),
            'xfn' => '',
        ),
      );
    
      $db_fields = false;
      // If your links will be hierarchical, adjust the $db_fields array below
      if ( false ) { 
        $db_fields = array( 'parent' => 'parent', 'id' => 'post_parent' ); 
      }
    
      $walker = new Walker_Nav_Menu_Checklist( $db_fields );
      $removed_args = array( 'action', 'customlink-tab', 'edit-menu-item', 'menu-item', 'page-tab', '_wpnonce', );
      ?>
      <div id="plugin-slug-div">
        <div id="tabs-panel-plugin-slug-all" class="tabs-panel tabs-panel-active">
        <ul id="plugin-slug-checklist-pop" class="categorychecklist form-no-clear" >
          <?php echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $my_items ), 0, (object) array( 'walker' => $walker ) ); ?>
        </ul>
        <p class="button-controls">
          <span class="add-to-menu">
            <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-plugin-slug-menu-item" id="submit-plugin-slug-div" />
            <span class="spinner"></span>
          </span>
        </p>
      </div>
      <?php
    }