Site icon Hip-Hop Website Design and Development

How to make add meta box for two kinds

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 correctly.

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 '<p><label for="show_in_slider">'.__('Show in Slider','').': </label>';

//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' : '' ).' /></p>';

  echo '<p><label for="show_in_homepage">Show in Homepage: </label>';

//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' : '' ).' /></p>';

  echo '<p><label for="video_type">'.__('Featured Type','').': </label><br/>';

  echo '<select id="video_type" name="dess_post[page_featured_type]"><option value="">Image</option><option value="youtube" '.($type == 'youtube' ? 'selected="selected"' : '').'>Youtube</option><option value="vimeo" '.($type == 'vimeo' ? 'selected="selected"' : '').'>Vimeo</option></select></p>';

  echo '<p><label for="video_id">'.__('Video ID','').': </label><br/>';

  echo '<input type="text" id="video_id" name="dess_post[page_video_id]" value="'.get_post_meta( $post->ID, 'page_video_id', true ).'" /></p>';

}

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' );