最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

metabox - I created a Custom Meta Box but it is not displaying the value on my post page

programmeradmin2浏览0评论

So i created a custom meta box, here is the code :

add_action( 'add_meta_boxes', 'register_meta_box' );
function register_meta_box(){
    add_meta_box( 'author_email', 'Author Email Id', 'meta_author_email', 'post', 'side', 'default' );
}

function meta_author_email($post){
    wp_nonce_field( 'author_save_email_data', 'email_meta_box_nonce' );

    $value = get_post_meta( $post->ID, '_author_email_value_key', true );

    echo '<label for="author_email_field">Author Email Address: </label>';

    echo '<input type="email" id="author_email_field" name="author_email_field" value="'. esc_attr( $value ) .'" size="25"/>';
}

function author_save_email_data($post_id){
    if (!isset($_POST['email_meta_box_nonce'])) {
        return;
    }
    if (!wp_verify_nonce( $_POST['email_meta_box_nonce'], author_save_email_data )) {
        return;
    }
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    if (!current_user_can( 'edit_post', $post_id )) {
        return;
    }
    if (!isset($_POST['author_email_field'])) {
        return;
    }
    $mydata = sanitize_text_field( $_POST['author_email_field'] );

    update_post_meta( $post_id, '_author_email_value_key', $mydata );
}
add_action( 'save_post', 'author_save_email_data' );

This is how i am calling it on my single page

<?php echo get_post_meta( get_the_ID(), 'author_email_field', true ); ?>
发布评论

评论列表(0)

  1. 暂无评论