カスタム メタ フィールドを追加して、いくつかの値を保存し、ループに表示しようとしています。カスタム フィールドは編集投稿に表示されますが、値は保存されません。多くの記事を読みましたが、解決策がわかりません。
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'] ) );
}
?>