WordPress自定义posttype元值不保存

时间:2014-07-25 作者:Jagankumar

我正在开发一个wp插件,它支持自定义帖子类型,因为我已经创建了一个metabox,一切都很顺利。当我在metabox字段中输入值时,它不会显示在管理面板中,因为它将存储在数据库中(wp\\U Posteta表)。有人能告诉我我的代码出了什么问题吗。

//Create Meta box
 function astest_add_metabox(){
 add_meta_box("astestimonial_metaboxes", "AS-Testimonial Client Information", "astestimonial_meta_box", "astestimonial", "side", "low");
 }
add_action(\'add_meta_boxes\',\'astest_add_metabox\');

//html code
function astestimonial_meta_box($post_id){
wp_nonce_field(\'astest_meta_box\',\'astest_meta_box_nonce\');
$clientname = get_post_meta($post->ID,\'clientname_value\',\'true\');
echo\'<label for="client_name_val">\';
_e(\'Name: \', \'astestimonial_textdomain\');
echo\'</label>\';
echo\'<input type="text" id="clientname_value" name="clientname_value"   value="\'.esc_attr($clientname).\'" />\';

//save post
function as_testi_save_data($post_id){
global $post;
if(isset($_POST[\'post_type\']) && ($_POST[\'post_type\'] == "astestimonial"))
{
    $cli_name_data = $_POST[\'clientname_value\'];

update_post_meta($post_id, \'clientname_value\', $cli_name_data);
 }
}

add_action("save_post", "as_testi_save_data");
有人能找出这有什么问题吗。。谢谢

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

您忘记申报全局$post

//Create Meta box
function astest_add_metabox() {
    add_meta_box("astestimonial_metaboxes", "AS-Testimonial Client Information",    "astestimonial_meta_box", "astestimonial", "side", "low");
}
add_action(\'add_meta_boxes\',\'astest_add_metabox\');

//html code
function astestimonial_meta_box() {
    global $post;
    wp_nonce_field(\'astest_meta_box\',\'astest_meta_box_nonce\');
    $clientname = get_post_meta($post->ID,\'clientname_value\',true);
    echo\'<label for="client_name_val">\';
    _e(\'Name: \', \'astestimonial_textdomain\');
    echo\'</label>\';
    echo\'<input type="text" id="clientname_value" name="clientname_value"   value="\'.esc_attr($clientname).\'" />\';
}

//save post
function as_testi_save_data($post_id){
    if(isset($_POST[\'post_type\']) && ($_POST[\'post_type\'] == "astestimonial"))  {
            $cli_name_data = $_POST[\'clientname_value\'];
            update_post_meta($post_id, \'clientname_value\', $cli_name_data);
    }
}

add_action("save_post", "as_testi_save_data");

结束