无法以自定义发布类型保存MetaBox数据

时间:2013-10-03 作者:Andrew

我在这个问题上工作了很长时间,真的很困惑。以下是我的自定义帖子类型的所有代码:

/* Services Post Type */

function services_post_type() {
$labels = array(
\'name\' => _x( \'Services\', \'service\' ),
\'singular_name\' => _x( \'Service\', \'service\' ),
\'add_new\' => _x( \'Add New\', \'service\' ),
\'add_new_item\' => _x( \'Add New Service\', \'service\' ),
\'edit_item\' => _x( \'Edit Service\', \'service\' ),
\'new_item\' => _x( \'New Service\', \'service\' ),
\'view_item\' => _x( \'View Service\', \'service\' ),
\'search_items\' => _x( \'Search Services\', \'service\' ),
\'not_found\' => _x( \'No services found\', \'service\' ),
\'not_found_in_trash\' => _x( \'No services found in Trash\', \'service\' ),
\'parent_item_colon\' => _x( \'Parent Service:\', \'service\' ),
\'menu_name\' => _x( \'Services\', \'service\' ),
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => false,
\'supports\' => array( \'title\', \'editor\' ),
\'taxonomies\' => array( \'category\' ),
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
\'has_archive\' => true,
\'query_var\' => true,
\'can_export\' => true,
\'rewrite\' => true,
\'capability_type\' => \'post\'
);
register_post_type( \'service\', $args );
} 
add_action( \'init\', \'services_post_type\' );

/*
 * Custom meta boxes 
*/

add_action( \'add_meta_boxes\', \'create_meta_boxes\' );

function create_meta_boxes() {
add_meta_box( \'my-meta-box-id\', __(\'Service Price\'), \'meta_box_info\', \'service\', \'normal\', \'low\' );
}


// Create meta box: Service Price

function meta_box_info( $post ) {

$values = get_post_custom( $post->ID );

wp_nonce_field( \'my_meta_box_nonce\', \'meta_box_nonce\' );
?>

<?php $text = get_post_meta($post->ID, \'meta_box_info\', true); ?>
<p>Price: <input type="text" name="meta_box_info" id="meta_box_info" size="25" /></p>

<?php   
}

// Save meta box: Service Price

function save_my_metadata($ID = false, $post = false)
{
if($post->post_type != \'service\')
    return;
update_post_meta($ID, \'my_metadata\', $_POST[\'my_metadata\']);
}

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

正如kaiser指出的,您需要使用save_post 钩住你的save_my_metadata() 作用

有关保存(和验证)metabox数据的完整示例,请参阅此代码示例:

https://github.com/Veraxus/nv-example-meta-box/blob/master/init.php

结束

相关推荐

将JSON对象属性保存到自定义Metabox

案例:我使用图像采集器。js脚本为我的自定义元框创建图像选择器。但这对WP 3.6已经不起作用了,所以我尝试用新的媒体上传器创建一个图像选择器元盒。我将其用于JSON对象,但如何将这些属性从JSON对象保存到metabox?jQuery图像选择器:jQuery(function(jQuery) { // Uploading files var file_frame; var wp_media_post_id = wp.media.model.set