php – How to make add meta box for two kinds

Question

I made meta box for two it shows in meta box but I think it does not work

for “ex_show_in_homepage” .

show_in_slider is working correctlty

Please help me

Thank you

add_action( ‘add_meta_boxes’, ‘dess_post_meta_box’ );

//「投稿」にメタボックスを追加する add_meta_boxes s 複数形

function dess_post_meta_box_callback( $post ) {

wp_nonce_field( ‘dess_post_save_meta_box_data’, ‘dess_post_meta_box_nonce’ );

//slider in slider post id in meta box
$show_in_slider = get_post_meta( $post->ID, ‘show_in_slider’, true );

//show iin homepage post id in meta box
$show_in_homepage = get_post_meta( $post->ID, ‘ex_show_in_homepage’, true );

//page_featured_type post id
$type = get_post_meta( $post->ID, ‘page_featured_type’, true );

//投稿編集画面のメタボックスhtml

echo ‘

‘.__(‘Show in Slider’,”).’: ‘;

//slider in slider post id from meta box
echo ‘<input type=”checkbox” id=”show_in_slider” name=”show_in_slider” value=”Yes” ‘.($show_in_slider == ‘Yes’ ? ‘checked’ : ” ).’ />

‘;

echo ‘

Show in Homepage: ‘;

//show in homepage post id from meta box
echo ‘<input type=”checkbox” id=”show_in_homepage” name=”ex_show_in_homepage” value=”Yes” ‘.($show_in_homepage == ‘Yes’ ? ‘checked’ : ” ).’ />

‘;

echo ‘

‘.__(‘Featured Type’,”).’:
‘;

echo ‘Image<option value=”youtube” ‘.($type == ‘youtube’ ? ‘selected=”selected”‘ : ”).’>Youtube<option value=”vimeo” ‘.($type == ‘vimeo’ ? ‘selected=”selected”‘ : ”).’>Vimeo

‘;

echo ‘

‘.__(‘Video ID’,”).’:
‘;

echo ‘ID, ‘page_video_id’, true ).'” />

‘;

}

function dess_post_save_meta_box_data( $post_id ) {

if ( ! isset( $_POST[‘dess_post_meta_box_nonce’] ) ) {

return;

}

if ( ! wp_verify_nonce( $_POST[‘dess_post_meta_box_nonce’], ‘dess_post_save_meta_box_data’ ) ) {

return;

}

if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE ) {

return;

}

if ( isset( $_POST[‘post_type’] ) && ‘page’ == $_POST[‘post_type’] ) {

if ( ! current_user_can( 'edit_page', $post_id ) ) {



  return;



}

} else {

if ( ! current_user_can( 'edit_post', $post_id ) ) {



  return;



}

}

$show_in_slider = sanitize_text_field( $_POST[‘show_in_slider’] );

$show_in_slider = sanitize_text_field( $_POST[‘ex_show_in_homepage’] );

update_post_meta( $post_id, ‘show_in_slider’, $show_in_slider );

update_post_meta( $post_id, ‘ex_show_in_homepage’, $show_in_homepage );

$arr = array();

if (isset($_POST[‘dess_post’])){

$arr = $_POST[‘dess_post’];

}

foreach ($arr as $key => $value) {

$val = sanitize_text_field($value);



update_post_meta( $post_id, $key, $val );

}

}

//投稿したメタボックスデータを取得、保存
add_action( ‘save_post’, ‘dess_post_save_meta_box_data’ );

0
kaz Oh 1 year 2021-11-24T13:20:47-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse