0

我正在尝试添加自定义元字段以保存一些值并在循环中显示。自定义字段显示在编辑帖子上,但不保存任何值。我已经阅读了很多文章,但无法找出解决方案。

add_action( 'add_meta_boxes', 'add_atomic_score' );
function add_atomic_score()  
{  
add_meta_box( 'score', 'Atomic Score:', 'atomic_score', 'post', 'side', 'high' );  
}
function atomic_score( $post ){
echo '<p>Enter Atomic Score Here: </p>';
$text = get_post_meta($post->ID, 'score', true);
    ?>  
<label for="score">Atomic Score: </label>  
<input type="text" name="score" id="score" value="<?php echo $text; ?>" />  
<?php  
}

add_action( 'save_post', 'atomic_reach_save' );  
function atomic_reach_save( $post_id )  
{  

if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; 


if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return; 


if( !current_user_can( 'edit_post' ) ) return;   


if( isset( $_POST['score'] ) )  
    update_post_meta( $post_id, 'score', esc_attr( $_POST['score'] ) );  

}
?>
4

0 回答 0