边栏Wordpress中的自定义菜单

边栏Wordpress中的自定义菜单,wordpress,menu,sidebar,Wordpress,Menu,Sidebar,我在侧边栏中显示自定义菜单时遇到问题。 现在,我已经从WordPress后端创建了一个菜单。此菜单必须显示在我的模板的侧栏中,带有菜单名,即自定义菜单和包含父菜单和子菜单的结构 我的目标是 目前,我有这部分代码: $meta_box_menu = array( 'id' => 'custom-meta-menu', 'title' => 'Menu Sidebar', 'page' => 'page', 'context' => 'side', 'priority' =&

我在侧边栏中显示自定义菜单时遇到问题。 现在,我已经从WordPress后端创建了一个菜单。此菜单必须显示在我的模板的侧栏中,带有菜单名,即自定义菜单和包含父菜单和子菜单的结构

我的目标是

目前,我有这部分代码:

$meta_box_menu = array(
'id' => 'custom-meta-menu',
'title' => 'Menu Sidebar',
'page' => 'page',
'context' => 'side',
'priority' => 'high',
'fields' => array(
    array(
        'id' => 'custom-meta-menu-name',
        'type' => 'select',
        'std' => 'none'
    ),
),
);

/*
* This function will register the meta box with WordPress
*/
function custom_add_box() {
    global $meta_box_menu;
    add_meta_box($meta_box_menu['id'], $meta_box_menu['title'], 'custom_meta_menu_html',
$meta_box_menu['page'], $meta_box_menu['context'], $meta_box_menu['priority']);
}
add_action('admin_init', 'custom_add_box');

/*
* This function will produce the html needed to display our meta box in the admin area
*/
function custom_meta_menu_html() {
  global $meta_box_menu, $post;

  $output = '<p style="padding:10px 0 0 0;">'.__('Scegli il menu da mostrare nella Sidebar di questa pagina.', 'custom').'</p>';
  $output .= '<input type="hidden" name="sf_meta_box_nonce" value="'.
wp_create_nonce(basename(__FILE__)). '" />';

  $output .= '<table class="form-table">';

  foreach ($meta_box_menu['fields'] as $field) {
    $meta = get_post_meta($post->ID, $field['id'], true);

/*
*   Get out all our menus using the function from functions.php
*/
$menus = custom_get_all_menus();

/*
*   Grab out saved data for edit mode
*/
$meta = get_post_meta($post->ID, $field['id'], true);

$output .= '<select name="'.$field['id'].'" class="widefat">';
$output .= '<option value="none">- none -</option>';
if(is_array($menus)):
  foreach($menus as $k => $v):
    if($meta==$v->slug):
      $output .= '<option selected="selected" value="' . $v->slug .'">' . $v->name . '</option>';
    else:
      $output .= '<option value="' . $v->slug .'">' . $v->name . '</option>';
    endif;
  endforeach;
endif;
$output .= '</select>';

  }

  $output .= '</table>';

  echo $output;
}

/*
* This function will save our preferences into the database
*/
function custom_save_data($post_id) {

  global $meta_box, $meta_box_menu;

foreach ($meta_box_menu['fields'] as $field) {
  $old = get_post_meta($post_id, $field['id'], true);
  $new = $_POST[$field['id']];

  if ($new && $new != $old) {
    update_post_meta($post_id, $field['id'], stripslashes(htmlspecialchars($new)));
  } elseif ('' == $new && $old) {
    delete_post_meta($post_id, $field['id'], $old);
  }
    }
}
add_action('save_post', 'custom_save_data');

function custom_get_all_menus() {
  $menu_obj = get_terms( 'nav_menu' );
  return $menu_obj;
}

/*
* Html Custom Sidebar Menu
*/
function custom_generate_menu_from_items($items) {
   if(count($items)>0):
    $menu_list = '<aside class="widget sidebar_menu"><h5 class="widget-title">' .'CUSTOM MENU'. '</h5><nav><ul class="sidebar-menu-items">';
  foreach ( (array) $items as $key => $menu_item ) {
    $title = $menu_item->title;
    $url = $menu_item->url;
    $menu_list .= '<li><a href="' . $url . '">' . $title . '</a></li>';
  }
$menu_list .= '</ul></nav></aside>';
return $menu_list;
$menu_item->title = get_post_meta( $menu_item->ID, '_menu_item_title', true );
    return $menu_item;
endif;
add_filter( 'wp_get_nav_menu_items', 'custom_generate_menu_from_items', null, 3 );
}
使用此代码,菜单显示在输出页面中,但页面位于同一级别,没有父子关系。 我怎样才能保持这种关系


谢谢你的支持

我想说,这里面没有逻辑来检查关系

var_dump($items);
然后查看是否存在与菜单项关系相关的字段。可能是类似于parentId或类似的东西

如果它不在那里,那么你必须自己查询它们

一旦有了它们,就可以检查它是否有parentId,并以这种方式处理格式。如果嵌套了多个级别,这将变得有点棘手,因为您必须开始检查父级是否有父级,并处理所有这些不同情况的格式设置