WordPress保存下拉元框



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

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

尝试更改这些

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声明为全局变量

相关内容

  • 没有找到相关文章

最新更新