首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为自定义帖子类型添加和创建多个meta框

为自定义帖子类型添加和创建多个meta框
EN

Stack Overflow用户
提问于 2020-09-01 22:40:02
回答 1查看 26关注 0票数 0

我正在尝试保存我创建的3个元框中的输入。下面是我的代码。我错过了什么,因为我只保存了smashing post-h2id 3次。

两个没有保存的元框是class post-class和class post-class1。输入字段只保存来自smashing post-v2的输入。

代码语言:javascript
复制
/* Fire our meta box setup function on the post editor screen. */
add_action( 'add_meta_boxes', 'smashing_add_post_meta_boxes' );
add_action( 'save_post', 'smashing_save_post_class_meta', 10, 2 );


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

    // sku,price, short description
    add_meta_box(
      'smashing-post-class',      // Unique ID
      esc_html__( 'SKU', 'SKU' ),    // Title
      'smashing_post_class_meta_box',   // Callback function
      'products',         // Admin page (or post type)
      'normal',         // Context
      'default'         // Priority
    );
    add_meta_box(
        'smashing-post-class1',      // Unique ID
        esc_html__( 'Price', 'Price' ),    // Title
        'smashing_post_class_meta_box',   // Callback function
        'products',         // Admin page (or post type)
        'normal',         // Context
        'default'         // Priority
      );
      add_meta_box(
        'smashing-post-classs2',      // Unique ID
        esc_html__( 'Short Description', 'Short Description' ),    // Title
        'smashing_post_class_meta_box',   // Callback function
        'products',         // Admin page (or post type)
        'normal',         // Context
        'default'         // Priority
      );

  }


/* Display the post meta box. */
function smashing_post_class_meta_box( $post ) { ?>

    <?php wp_nonce_field( basename( __FILE__ ), 'smashing_post_class_nonce' ); ?>
  
    <p>
      <label for="smashing-post-class"><?php _e( "Add meta tag", 'example' ); ?></label>
      <br />
      <input class="widefat" type="text" name="smashing-post-class" id="smashing-post-class" value="<?php echo esc_attr( get_post_meta( $post->ID, 'smashing_post_class', true ) ); ?>" size="30" />
    </p>
    
  <?php }


/* Save the meta box’s post metadata. */
function smashing_save_post_class_meta( $post_id, $post ) {

    /* Verify the nonce before proceeding. */
    if ( !isset( $_POST['smashing_post_class_nonce'] ) || !wp_verify_nonce( $_POST['smashing_post_class_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['smashing-post-class'] ) ? sanitize_html_class( $_POST['smashing-post-class'] ) : ’ );
  
    /* Get the meta key. */
    $meta_key = 'smashing_post_class';
  
    /* 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 );      
 
  
  }
EN

回答 1

Stack Overflow用户

发布于 2020-09-01 23:47:26

您必须为每个输入创建一个单独的元字段。在您的示例中,您刚刚创建了一个输入并对其进行了更新。

您可以在“save_post”钩子中尝试此操作

代码语言:javascript
复制
update_post_meta( $post_id, 'smashing_post_class_2', $_POST['smashing_post_class_2'] );
update_post_meta( $post_id, 'smashing_post_class_3', $_POST['smashing_post_class_3'] );

如果字段不存在,update_post_meta()函数将创建该字段,因此您不需要创建它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63689907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档