解决方法是add_meta_box
在里面functions.php
. 这是我的代码:
add_action( \'add_meta_boxes\', \'my_custom_box\' );
function my_custom_box(){
if ( function_exists(\'add_meta_box\') ) {
add_meta_box( \'page_custom_menu\',\'page-menu\', \'page_custom_menu_box\', \'page\', \'side\',\'high\');
add_meta_box(\'page_custom_menu\',\'page-menu\', \'page_custom_menu_box\', \'post\', \'side\', \'high\');
}
}
function page_custom_menu_box(){
global $post;
if ( metadata_exists( \'post\', $post->ID, \'page_menu\' ) ) {
$menu_id = get_post_meta( $post->ID, \'page_menu\', true );
}
$entries = get_terms( \'nav_menu\', array( \'hide_empty\' => false ) );
echo \'<select class="postform" id="page_menu" name="page_menu"> \';
echo \'<option value="">Select...</option> \';
foreach ($entries as $key => $entry){
$id = $entry->term_id;
$title = $entry->name;
if ( $id == $menu_id ){
$selected = "selected=\'selected\'";
}else{
$selected = "";
}
echo"<option $selected value=\'". $id."\'>". $title."</option>";
}
echo \'</select>\';
}
add_action(\'save_post\', \'save_postdata\');
function save_postdata( $post_id ) {
global $post;
if( !isset($_POST[\'page_menu\']) )
return;
$data = $_POST[\'page_menu\'];
if(get_post_meta($post_id, \'page_menu\') == "") {
add_post_meta($post_id, \'page_menu\', $data, true);
}elseif($data != get_post_meta($post_id, \'page_menu\', true)) {
update_post_meta($post_id, \'page_menu\', $data);
}elseif($data == "") {
delete_post_meta($post_id, \'page_menu\', get_post_meta($post_id, \'page_menu\', true));
}
}
在中
header.php
:
if( is_page() && get_post_meta( $post->ID, \'page_menu\', true )!=\'\' ){
global $post;
$menu_id = get_post_meta( $post->ID, \'page_menu\', true );
wp_nav_menu( array( \'container_id\' => \'submenu\', \'menu\' => $menu_id ) );
}elseif( is_single() && get_post_meta( $post->ID, \'page_menu\', true )!=\'\' ){
global $post;
$menu_id = get_post_meta( $post->ID, \'page_menu\', true );
wp_nav_menu( array( \'container_id\' => \'submenu\', \'menu\' => $menu_id ) );
}else{
//default menu
wp_nav_menu( array( \'container_id\' => \'submenu\', \'theme_location\' => \'primary\',\'menu_id\'=>\'web2feel\' ,\'menu_class\'=>\'sfmenu\',\'fallback_cb\'=> \'fallbackmenu\' ) );
}