Php Wordpress自定义元框未保存所有数据

Php Wordpress自定义元框未保存所有数据,php,wordpress,custom-post-type,meta,Php,Wordpress,Custom Post Type,Meta,我有下面的代码添加两个独立的元框到我的自定义文章类型。最初我只有“推荐”元框,它工作得很好。我现在添加了“积分”,它在管理中显示良好,但似乎没有保存到数据库中 谁能帮忙,希望没什么大不了的 // Allow registering of custom meta boxes add_action( 'add_meta_boxes', 'add_project_metaboxes' ); // Register the Project Meta Boxes function add_project

我有下面的代码添加两个独立的元框到我的自定义文章类型。最初我只有“推荐”元框,它工作得很好。我现在添加了“积分”,它在管理中显示良好,但似乎没有保存到数据库中

谁能帮忙,希望没什么大不了的

// Allow registering of custom meta boxes
add_action( 'add_meta_boxes', 'add_project_metaboxes' );

// Register the Project Meta Boxes
function add_project_metaboxes() {
    add_meta_box('oak_testimonial', 'Testimonial', 'oak_testimonial', 'project', 'normal', 'default');
    add_meta_box('oak_credits', 'Credits', 'oak_credits', 'project', 'side', 'default');
}

// Add Testimonial Metabox
function oak_testimonial() {
    global $post;

    // Noncename needed to verify where the data originated
    echo '<input type="hidden" name="testimonialmeta_noncename" id="testimonialmeta_noncename" value="' . 
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

    // Get the testimonial data if its already been entered
    $quote = get_post_meta($post->ID, '_quote', true);
    $name = get_post_meta($post->ID, '_name', true);
    $title = get_post_meta($post->ID, '_title', true);
    $company = get_post_meta($post->ID, '_company', true);

    // Echo out the field
    echo '<p>Name:</p>';
    echo '<input type="text" name="_name" value="' . $name  . '" class="widefat" />';
    echo '<p>Job Title:</p>';
    echo '<input type="text" name="_title" value="' . $title  . '" class="widefat" />';
    echo '<p>Company:</p>';
    echo '<input type="text" name="_company" value="' . $company  . '" class="widefat" />';
    echo '<p>Quote:</p>';
    echo '<input type="text" name="_quote" value="' . $quote  . '" class="widefat" />';

}

// Add Credits Metabox
function oak_credits() {
    global $post;

    // Noncename needed to verify where the data originated
    echo '<input type="hidden" name="creditsmeta_noncename" id="creditsmeta_noncename" value="' . 
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

    // Get the credits data if its already been entered
    $role1 = get_post_meta($post->ID, '_role1', true);
    $name1 = get_post_meta($post->ID, '_name1', true);
    $role2 = get_post_meta($post->ID, '_role2', true);
    $name2 = get_post_meta($post->ID, '_name2', true);
    $role3 = get_post_meta($post->ID, '_role3', true);
    $name3 = get_post_meta($post->ID, '_name3', true);

    // Echo out the field
    echo '<p>Role:</p>';
    echo '<input type="text" name="_name" value="' . $role1  . '" class="widefat" />';
    echo '<p>Name:</p>';
    echo '<input type="text" name="_title" value="' . $name1  . '" class="widefat" />';
    echo '<p>Role:</p>';
    echo '<input type="text" name="_name" value="' . $role2  . '" class="widefat" />';
    echo '<p>Name:</p>';
    echo '<input type="text" name="_title" value="' . $name2  . '" class="widefat" />';
    echo '<p>Role:</p>';
    echo '<input type="text" name="_name" value="' . $role3  . '" class="widefat" />';
    echo '<p>Name:</p>';
    echo '<input type="text" name="_title" value="' . $name3  . '" class="widefat" />';

}

// Save the Metabox Data
function oak_save_project_meta($post_id, $post) {

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if ( !wp_verify_nonce( $_POST['testimonialmeta_noncename'], plugin_basename(__FILE__) )) {
    return $post->ID;
    }
    if ( !wp_verify_nonce( $_POST['creditsmeta_noncename'], plugin_basename(__FILE__) )) {
    return $post->ID;
    }

    // Is the user allowed to edit the post or page?
    if ( !current_user_can( 'edit_post', $post->ID ))
        return $post->ID;

    // OK, we're authenticated: we need to find and save the data
    // We'll put it into an array to make it easier to loop though.

    $testimonial_meta['_quote'] = $_POST['_quote'];
    $testimonial_meta['_name'] = $_POST['_name'];
    $testimonial_meta['_title'] = $_POST['_title'];
    $testimonial_meta['_company'] = $_POST['_company'];

    $credit_meta['_role1'] = $_POST['_role1'];
    $credit_meta['_name1'] = $_POST['_name1'];
    $credit_meta['_role2'] = $_POST['_role2'];
    $credit_meta['_name2'] = $_POST['_name2'];
    $credit_meta['_role3'] = $_POST['_role3'];
    $credit_meta['_name3'] = $_POST['_name3'];

    // Add values of $testimonial_meta as custom fields
    foreach ($testimonial_meta as $key => $value) { // Cycle through the $testimonial_meta array
        if( $post->post_type == 'revision' ) return; // Don't store custom data twice
        $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
        if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
            update_post_meta($post->ID, $key, $value);
        } else { // If the custom field doesn't have a value
            add_post_meta($post->ID, $key, $value);
        }
        if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
    }

    // Add values of $credit_meta as custom fields
    foreach ($credit_meta as $key => $value) { // Cycle through the $testimonial_meta array
        if( $post->post_type == 'revision' ) return; // Don't store custom data twice
        $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
        if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
            update_post_meta($post->ID, $key, $value);
        } else { // If the custom field doesn't have a value
            add_post_meta($post->ID, $key, $value);
        }
        if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
    }

}

add_action('save_post', 'oak_save_project_meta', 1, 2); // save the custom fields
//允许注册自定义元框
添加动作(“添加元框”、“添加项目元框”);
//注册项目元框
函数添加\项目\元框(){
添加元框(“oak_认证”、“认证”、“oak_认证”、“项目”、“正常”、“默认”);
添加“元”框(“橡树学分”、“学分”、“橡树学分”、“项目”、“侧边”、“默认值”);
}
//添加推荐元数据库
函数oak_证明(){
全球$员额;
//Noncename需要验证数据的来源
回声';
//如果已输入证明数据,则获取证明数据
$quote=get_post_meta($post->ID,'u quote',true);
$name=get_post_meta($post->ID,'u name',true);
$title=get_post_meta($post->ID,'u title',true);
$company=get_post_meta($post->ID,'u company',true);
//在田野里回响
回音“名称:

”; 回声'; 回声“职务:

”; 回声'; 回声公司:

; 回声'; 回音“引用:

”; 回声'; } //添加积分元盒 函数oak_credits(){ 全球$员额; //Noncename需要验证数据的来源 回声'; //如果已输入信用数据,则获取信用数据 $role1=get_post_meta($post->ID,'u role1',true); $name1=get_post_meta($post->ID,'u name1',true); $role2=get_post_meta($post->ID,'u role2',true); $name2=get_post_meta($post->ID,'u name2',true); $role3=get_post_meta($post->ID,'u role3',true); $name3=get_post_meta($post->ID,'u name3',true); //在田野里回响 回显“角色:

”; 回声'; 回音“名称:

”; 回声'; 回显“角色:

”; 回声'; 回音“名称:

”; 回声'; 回显“角色:

”; 回声'; 回音“名称:

”; 回声'; } //保存Metabox数据 函数oak\u save\u project\u meta($post\u id,$post){ //确认此信息来自我们的屏幕并经过适当授权, //因为save_post可以在其他时间触发 如果(!wp_verify_nonce($_POST['CertificationMeta_noncename'],plugin_basename(uu文件_u))){ 返回$post->ID; } 如果(!wp\u verify\u nonce($\u POST['creditsmeta\u noncename'],plugin\u basename(\uuuu FILE\uuuu))){ 返回$post->ID; } //是否允许用户编辑文章或页面? 如果(!当前用户\u可以('edit\u post',$post->ID)) 返回$post->ID; //好的,我们已通过身份验证:我们需要查找并保存数据 //我们将把它放入一个数组中,以使循环更容易。 $commential_meta[''u quote']=$u POST['u quote']; $commentional_meta[''u name']=$u POST['u name']; $commentional_meta[“u title”]=$_POST[“u title”]; $commentional_meta[“u company”]=$u POST[“u company”]; $credit_meta[''u role1']=$u POST['u role1']; $credit_meta[''u name1']=$u POST['u name1']; $credit_meta[''u role2']=$u POST['u role2']; $credit_meta[''u name2']=$u POST['u name2']; $credit_meta[''u role3']=$u POST['u role3']; $credit_meta[''u name3']=$u POST['u name3']; //将$ESTIONAL_meta的值添加为自定义字段 foreach($testional_meta as$key=>$value){//循环遍历$testional_meta数组 if($post->post_type=='revision')返回;//不存储自定义数据两次 $value=内爆(“,”,(数组)$value);//如果$value是数组,则将其设置为CSV(不太可能) if(get_post_meta($post->ID,$key,FALSE)){//如果自定义字段已经有值 更新发布元数据($post->ID,$key,$value); }else{//如果自定义字段没有值 添加帖子元($post->ID,$key,$value); } if(!$value)delete_post_meta($post->ID,$key);//如果为空则删除 } //将$credit\u meta的值添加为自定义字段 foreach($key=>$value)遍历$commential\u元数组 if($post->post_type=='revision')返回;//不存储自定义数据两次 $value=内爆(“,”,(数组)$value);//如果$value是数组,则将其设置为CSV(不太可能) if(get_post_meta($post->ID,$key,FALSE)){//如果自定义字段已经有值 更新发布元数据($post->ID,$key,$value); }else{//如果自定义字段没有值 添加帖子元($post->ID,$key,$value); } if(!$value)delete_post_meta($post->ID,$key);//如果为空则删除 } } 添加动作('save_post','oak_save_project_meta',1,2);//保存自定义字段
您的“oak\u credits”元数据库的“名称”错误:

// Add Credits Metabox
function oak_credits() {
    global $post;

    // Noncename needed to verify where the data originated
    echo '<input type="hidden" name="creditsmeta_noncename" id="creditsmeta_noncename" value="' . 
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

    // Get the credits data if its already been entered
    $role1 = get_post_meta($post->ID, '_role1', true);
    $name1 = get_post_meta($post->ID, '_name1', true);
    $role2 = get_post_meta($post->ID, '_role2', true);
    $name2 = get_post_meta($post->ID, '_name2', true);
    $role3 = get_post_meta($post->ID, '_role3', true);
    $name3 = get_post_meta($post->ID, '_name3', true);

   // Echo out the field
    echo '<p>Role:</p>';
    echo '<input type="text" name="_role1" value="' . $role1  . '" class="widefat" />';
    echo '<p>Name:</p>';
    echo '<input type="text" name="_name1" value="' . $name1  . '" class="widefat" />';
    echo '<p>Role:</p>';
    echo '<input type="text" name="_role2" value="' . $role2  . '" class="widefat" />';
    echo '<p>Name:</p>';
    echo '<input type="text" name="_name2" value="' . $name2  . '" class="widefat" />';
    echo '<p>Role:</p>';
    echo '<input type="text" name="_role3" value="' . $role3  . '" class="widefat" />';
    echo '<p>Name:</p>';
    echo '<input type="text" name="_name3" value="' . $name3  . '" class="widefat" />';

}
//添加积分元盒
函数oak_credits(){
全球$员额;
//Noncename需要验证数据的来源
回声';
//如果已输入信用数据,则获取信用数据
$role1=get_post_meta($post->ID,'u role1',true);
$name1=get_post_meta($post->ID,'u name1',true);
$role2=get_post_meta($post->ID,'u role2',true);
$name2=get_post_meta($post->ID,'u name2',true);
$role3=get_post_meta($post->ID,'u role3',true);
$name3=get_post_meta($post->ID,'u name3',true);
//在田野里回响
回显“角色:

”; 回声'; 回音“名称:

”; 回声'; 回显“角色:

”; 回声'; 回音“名称:

”; 回声'; 回显“角色:

”; 回声'; 回音“名称:

”; 回声'; }
谢谢,犯了一个多么愚蠢的错误,看了太久了!您可能知道如何将标点符号(即“:”)附加到特定元代码的末尾吗?谢谢