WordPress Save Dropdown Metabox

时间:2018-02-02 10:04:44

标签: php wordpress dropdown savechanges meta-boxes

我已经在自定义帖子类型中创建了自定义元框。 所以我创建下拉列表以选择教师的名字和班级。 该值可以显示在下拉列表中,但在发布帖子后,该值无法保存并且下拉列表再次设置为默认值。 如果我在这里错过了部分或错误的代码,有人可以知道吗? 谢谢,如果你想要帮助,那么这是我的代码:

function portfolio_student(){
add_meta_box( 'portfolio_student', 'Select The Teachers and Classes', 'meta_box_data', 'portfolio', 'normal', 'high' ); }

function meta_box_data(){
// $post is already set, and contains an object: the WordPress post
global $post;
$values = get_post_custom( $post->ID );
$teachers = isset( $values['the_teachers'] ) ? esc_attr( $values['the_teachers'] ) : '';
$classes = isset( $values['the_classes'] ) ? esc_attr( $values['the_classes'] ) : '';

// We'll use this nonce field later on when saving.
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_data' );
?>
<p>
    <label for="the_teachers">Teacher</label>
    <select name="the_teachers" id="the_teachers">
      <option value="0">-- Select Class --</option>
      <?php
        $args = array(
          'numberposts' => -1,
          'post_type' => 'teacher'
        );
        $teacher_posts = get_posts($args);
        foreach( $teacher_posts as $post ) : setup_postdata($post); ?>
          <option value="<?php echo $post->ID; ?>" <?php selected($teachers, $post->ID); ?>><?php the_title(); ?></option>
        <?php endforeach; ?>
    </select>
</p>
<p>
    <label for="the_classes">Class</label>
    <select name="the_classes" id="the_classes">
      <option value="0">-- Select Class --</option>
      <?php
        $args = array(
          'numberposts' => -1,
          'post_type' => 'class'
        );
        $class_posts = get_posts($args);
        foreach( $class_posts as $post ) : setup_postdata($post); ?>
          <option value="<?php echo $post->ID; ?>" <?php selected($classes, $post->ID); ?>><?php the_title(); ?></option>
        <?php endforeach; ?>
    </select>
</p>
<?php}

add_action( 'save_post', 'cd_meta_box_save' );function cd_meta_box_save( $post_id ){
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  return;

// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_data'] ) || !wp_verify_nonce( $_POST['meta_box_data'], 'my_meta_box_nonce' ) )
  return;

// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) )
  return;

// Make sure your data is set before trying to save it
if( isset( $_POST['the_teachers'] ) )
    //Update Meta Data
    $teacher_meta = $_POST['the_teachers'];
    //EndUpdate
    update_post_meta( $post_id, 'the_teachers', $teacher_meta );

if( isset( $_POST['the_classes'] ) )
    //Update Meta Data
    $class_meta = $_POST['the_classes'];
    //EndUpdate
    update_post_meta( $post_id, 'the_classes', $class_meta ); }

1 个答案:

答案 0 :(得分:0)

尝试更改这些

update_post_meta( $post_id, 'the_teachers', $teacher_meta );

update_post_meta( $post_id, 'the_classes', $class_meta );

update_post_meta( $post->ID, 'the_teachers', $teacher_meta );

update_post_meta( $post->ID, 'the_classes', $class_meta );

由于您已将$post声明为全局变量