如何复制(多元框)?

时间:2016-05-04 作者:Max

我已经创建了一个元框字段,效果很好,但现在我需要再添加一个。在过去的几个小时里,我试着把它的每一部分都混合起来,复制它,取一些部分,谷歌,搜索,但没有运气。第二个元框出现,但不会保存。这是我的第一个元盒。有人能帮忙再做一个吗。需要从第一个添加到第二个/或删除的内容。有些零件在这里显示不好,所以版本比较干净http://pastebin.com/hYiYj1ZH

/* Fire our meta box setup function on the post editor screen. */
add_action( \'load-post.php\', \'sw_post_meta_boxes_setup\' );
add_action( \'load-post-new.php\', \'sw_post_meta_boxes_setup\' );

/* Meta box setup function. */
function sw_post_meta_boxes_setup() {

  /* Add meta boxes on the \'add_meta_boxes\' hook. */
  add_action( \'add_meta_boxes\', \'sw_add_post_meta_boxes\' );

  /* Save post meta on the \'save_post\' hook. */
  add_action( \'save_post\', \'sw_save_video_box_meta\', 10, 2 );

}

/* Create one or more meta boxes to be displayed on the post editor screen. */
function sw_add_post_meta_boxes() {

  add_meta_box(
    \'sw_video_player\',      // Unique ID
    esc_html__( \'Video Box\', \'video-text\' ),    // Title
    \'sw_video_box_meta_box\',   // Callback function
    \'job_listing\',// Admin page (custom post type)
    \'normal\',       // Context
    \'high\'          // Priority
  );

    add_meta_box(
    \'sw_face_player\',      // Unique ID
    esc_html__( \'Facebook Event Box\', \'video-text\' ),    // Title
    \'sw_face_box_meta_box\',   // Callback function
    \'job_listing\',// Admin page (custom post type)
    \'normal\',       // Context
    \'high\'          // Priority
  );


}

/* Display the post meta box. */
if ( !function_exists( \'sw_video_box_meta_box\' ) ) {
function sw_video_box_meta_box( $object, $box ) { 

  wp_nonce_field( basename( __FILE__ ), \'sw_video_box_nonce\' ); 

    
_e( "Place Video/Playlist Embed Code Here.", \'sw-text\' ); 

    
echo esc_html__( get_post_meta( $object->ID, \'sw_video_box\', true ) ); } } /* Save the meta box\'s post metadata. */ if ( !function_exists( \'sw_save_video_box_meta\' ) ) { function sw_save_video_box_meta( $post_id, $post ) { /* Verify the nonce before proceeding. */ if ( !isset( $_POST[\'sw_video_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'sw_video_box_nonce\'], basename( __FILE__ ) ) ) return $post_id; /* Get the post type object. */ $post_type = get_post_type_object( $post->post_type ); /* Check if the current user has permission to edit the post. */ if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) return $post_id; /* Get the posted data and sanitize it for use as an HTML class. */ $new_meta_value = ( isset( $_POST[\'sw_video_player\'] ) ? balanceTags( $_POST[\'sw_video_player\'] ) : \'\' ); /* Get the meta key. */ $meta_key = \'sw_video_box\'; /* Get the meta value of the custom field key. */ $meta_value = get_post_meta( $post_id, $meta_key, true ); /* If a new meta value was added and there was no previous value, add it. */ if ( $new_meta_value && \'\' == $meta_value ) add_post_meta( $post_id, $meta_key, $new_meta_value, true ); /* If the new meta value does not match the old value, update it. */ elseif ( $new_meta_value && $new_meta_value != $meta_value ) update_post_meta( $post_id, $meta_key, $new_meta_value ); /* If there is no new meta value but an old value exists, delete it. */ elseif ( \'\' == $new_meta_value && $meta_value ) delete_post_meta( $post_id, $meta_key, $meta_value ); } }

1 个回复
最合适的回答,由SO网友:Howard E 整理而成

您没有为facebook元框添加保存元操作。

<?php
/* Fire our meta box setup function on the post editor screen. */
add_action( \'load-post.php\', \'sw_post_meta_boxes_setup\' );
add_action( \'load-post-new.php\', \'sw_post_meta_boxes_setup\' );

/* Meta box setup function. */
function sw_post_meta_boxes_setup() {

  /* Add meta boxes on the \'add_meta_boxes\' hook. */
  add_action( \'add_meta_boxes\', \'sw_add_post_meta_boxes\' );

  /* Save post meta on the \'save_post\' hook. */
  add_action( \'save_post\', \'sw_save_video_box_meta\', 10, 2);
  add_action( \'save_post\', \'sw_save_face_box_meta\', 10, 2);

}

/* Create one or more meta boxes to be displayed on the post editor screen. */
function sw_add_post_meta_boxes() {

  add_meta_box(
    \'sw_video_player\',      // Unique ID
    esc_html__( \'Video Box\', \'video-text\' ),    // Title
    \'sw_video_box_meta_box\',   // Callback function
    \'job_listing\',// Admin page (custom post type)
    \'normal\',       // Context
    \'high\'          // Priority
  );

    add_meta_box(
    \'sw_face_player\',      // Unique ID
    esc_html__( \'Facebook Event Box\', \'video-text\' ),    // Title
    \'sw_face_box_meta_box\',   // Callback function
    \'job_listing\',// Admin page (custom post type)
    \'normal\',       // Context
    \'high\'          // Priority
  );


}

/* Display the post meta box. */
if ( !function_exists( \'sw_video_box_meta_box\' ) ) {
function sw_video_box_meta_box( $object, $box ) { ?>

  <?php wp_nonce_field( basename( __FILE__ ), \'sw_video_box_nonce\' ); ?>

  <p>
    <label for="sw_video_player"><?php _e( "Place Video/Playlist Embed Code Here.", \'sw-text\' ); ?></label>
    <br />
    <textarea class="widefat" name="sw_video_player" id="sw_video_player" cols="50" rows="5"><?php echo esc_html__( get_post_meta( $object->ID, \'sw_video_box\', true ) ); ?></textarea>
  </p>

<?php }

// Facebook Meta Box
if ( !function_exists( \'sw_face_box_meta_box\' ) ) {
function sw_face_box_meta_box( $object, $box ) { ?>

  <?php wp_nonce_field( basename( __FILE__ ), \'sw_face_box_nonce\' ); ?>

  <p>
    <label for="sw_face_player"><?php _e( "Place FB Video/Playlist Embed Code Here.", \'sw-text\' ); ?></label>
    <br />
    <textarea class="widefat" name="sw_face_player" id="sw_face_player" cols="50" rows="5"><?php echo esc_html__( get_post_meta( $object->ID, \'sw_face_box\', true ) ); ?></textarea>
  </p>

<?php }

    }
}


/* Save the meta box\'s post metadata. */
if ( !function_exists( \'sw_save_video_box_meta\' ) ) {
function sw_save_video_box_meta( $post_id, $post ) {

  /* Verify the nonce before proceeding. */
  if ( !isset( $_POST[\'sw_video_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'sw_video_box_nonce\'], basename( __FILE__ ) ) )
    return $post_id;

  /* Get the post type object. */
  $post_type = get_post_type_object( $post->post_type );

  /* Check if the current user has permission to edit the post. */
  if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
    return $post_id;

  /* Get the posted data and sanitize it for use as an HTML class. */
  $new_meta_value = ( isset( $_POST[\'sw_video_player\'] ) ? balanceTags( $_POST[\'sw_video_player\'] ) : \'\' );

  /* Get the meta key. */
  $meta_key = \'sw_video_box\';

  /* Get the meta value of the custom field key. */
  $meta_value = get_post_meta( $post_id, $meta_key, true );

  /* If a new meta value was added and there was no previous value, add it. */
  if ( $new_meta_value && \'\' == $meta_value )
    add_post_meta( $post_id, $meta_key, $new_meta_value, true );

  /* If the new meta value does not match the old value, update it. */
  elseif ( $new_meta_value && $new_meta_value != $meta_value )
    update_post_meta( $post_id, $meta_key, $new_meta_value );

  /* If there is no new meta value but an old value exists, delete it. */
  elseif ( \'\' == $new_meta_value && $meta_value )
    delete_post_meta( $post_id, $meta_key, $meta_value );
    } 
}

/* Save the meta box\'s post metadata. Facebook Box */
if ( !function_exists( \'sw_save_face_box_meta\' ) ) {
function sw_save_face_box_meta( $post_id, $post ) {

  /* Verify the nonce before proceeding. */
  if ( !isset( $_POST[\'sw_face_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'sw_face_box_nonce\'], basename( __FILE__ ) ) )
    return $post_id;

  /* Get the post type object. */
  $post_type = get_post_type_object( $post->post_type );

  /* Check if the current user has permission to edit the post. */
  if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
    return $post_id;

  /* Get the posted data and sanitize it for use as an HTML class. */
  $new_meta_value = ( isset( $_POST[\'sw_face_player\'] ) ? balanceTags( $_POST[\'sw_face_player\'] ) : \'\' );

  /* Get the meta key. */
  $meta_key = \'sw_face_box\';

  /* Get the meta value of the custom field key. */
  $meta_value = get_post_meta( $post_id, $meta_key, true );

  /* If a new meta value was added and there was no previous value, add it. */
  if ( $new_meta_value && \'\' == $meta_value )
    add_post_meta( $post_id, $meta_key, $new_meta_value, true );

  /* If the new meta value does not match the old value, update it. */
  elseif ( $new_meta_value && $new_meta_value != $meta_value )
    update_post_meta( $post_id, $meta_key, $new_meta_value );

  /* If there is no new meta value but an old value exists, delete it. */
  elseif ( \'\' == $new_meta_value && $meta_value )
    delete_post_meta( $post_id, $meta_key, $meta_value );
    } 
}

相关推荐

如何在WordPress开发中添加带有ACF自定义字段ID的自定义metabox字段

我是wordpress开发的新手,我在我的项目中安装了高级自定义字段插件,并创建了两个文本字段名称&;我还创建了一个插件,可以在帖子中创建一个带有文本框的元框。现在在帖子中,我将获得自定义字段名称(&A);电子邮件和我的自定义元框旁边将出现,但我必须将我的元框附加到名称字段旁边,即在名称字段和电子邮件字段之间。我的metabox代码如下。请任何人帮帮我//Creating the custom meta box function my_notice_meta_box() {