我创建了一个名为免费赠品的自定义帖子类型,因为我创建了一个自定义元部分,其中添加了一个输入字段。哪个不存储在该字段中输入的数据也不显示在该字段中输入的值。我附上了编码。
function adding_freebie_metabox( $post ) {
add_meta_box( 'my-meta-box',__( 'Freebie extra deatails', 'lwprjs' ),'render_my_freebie_metabox','freebie','normal','default');
}
add_action( 'add_meta_boxes_freebie', 'adding_freebie_metabox' );
//Add field
function render_my_freebie_metabox( $meta_id ) {
// make sure the form request comes from WordPress
wp_nonce_field( basename( __FILE__ ), 'freebie_meta_box_nonce' );
?>
Enter freebie details such as URL of download and also demo URL
<table class="form-table"><tbody>
<tr>
<th><label for="freebie-demo">Demo URL</label></th>
<td><input style="width: 100%" id="freebie-demo" name="freebie-demo" type="text" value="<?php get_post_meta( $post->ID, $meta_field['freebie-demo'], true ); ?>"></td>
</tr>
</tbody></table>
<?php
}
function food_save_meta_box_data( $post_id ){
// verify meta box nonce
if ( !isset( $_POST['freebie_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['freebie_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
// store custom fields values
// cholesterol string
if ( isset( $_REQUEST['freebie-demo'] ) ) {
update_post_meta( $post_id, '_freebie_demo', sanitize_text_field( $_POST['freebie-demo'] ) );
}
}
add_action( 'save_post_freebie', 'food_save_meta_box_data' );
?>
答案 0 :(得分:0)
我重写了整个块,请检查它是否现在有效:
function adding_freebie_metabox( $post ) {
add_meta_box( 'my-meta-box',__( 'Freebie extra deatails', 'lwprjs' ),'render_my_freebie_metabox','freebie','normal','default');
}
add_action( 'add_meta_boxes_freebie', 'adding_freebie_metabox' );
//Add field
function render_my_freebie_metabox( $post ) {
// make sure the form request comes from WordPress
wp_nonce_field( basename( __FILE__ ), 'freebie_meta_box_nonce' );
?>
Enter freebie details such as URL of download and also demo URL
<table class="form-table"><tbody>
<tr>
<th><label for="freebie-demo">Demo URL</label></th>
<td><input style="width: 100%" id="freebie-demo" name="freebie-demo" type="text" value="<?php get_post_meta( $post->ID, '_freebie_demo', true ); ?>"></td>
</tr>
</tbody></table>
<?php
}
function food_save_meta_box_data( $post_id ){
// verify meta box nonce
if ( !isset( $_POST['freebie_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['freebie_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
// store custom fields values
// cholesterol string
if ( isset( $_REQUEST['freebie-demo'] ) ) {
update_post_meta( $post_id, '_freebie_demo', sanitize_text_field( $_POST['freebie-demo'] ) );
}
}
add_action( 'save_post_freebie', 'food_save_meta_box_data' );
?>
答案 1 :(得分:0)
你正在保存:
freebie_demo
然后用这个检索:
freebie-demo
因此,请尝试将保存代码更改为:
if ( isset( $_POST['freebie-demo'] ) ) {
update_post_meta( $post_id, 'freebie-demo', sanitize_text_field( $_POST['freebie-demo'] ) );
}