我在侧边栏中显示自定义菜单时遇到问题。现在,我从 WordPress 后端创建了一个菜单。这个菜单必须显示在我的模板的侧边栏中,带有菜单名称(即 CUSTOM MENU)以及带有父母和孩子的结构。
我的目标是这个。
目前,我有这部分代码:
$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 );
}
使用此代码,菜单显示在输出页面中,但页面放置在同一级别,没有父子关系。我怎样才能保持这种关系?
谢谢你的支持。