Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress元框复选框更新未更新_Php_Wordpress_Meta Boxes - Fatal编程技术网

Php Wordpress元框复选框更新未更新

Php Wordpress元框复选框更新未更新,php,wordpress,meta-boxes,Php,Wordpress,Meta Boxes,我在更新wordpress中的自定义元框以获取自定义新闻帖子时遇到问题。我做了一个元框复选框,这样我就可以决定这篇文章是否有特色。如果选中该复选框,则isFeatured=1。在这部分工作之前,问题是什么时候过了一段时间我不想编辑文章并取消选中复选框。更新帖子后,复选框始终处于选中状态 数据库:meta_key | meta_value->news_details | a:1:{s:10:isFeatured;s:1:1;} 这就是我的代码的外观: function add_news_detai

我在更新wordpress中的自定义元框以获取自定义新闻帖子时遇到问题。我做了一个元框复选框,这样我就可以决定这篇文章是否有特色。如果选中该复选框,则isFeatured=1。在这部分工作之前,问题是什么时候过了一段时间我不想编辑文章并取消选中复选框。更新帖子后,复选框始终处于选中状态

数据库:meta_key | meta_value->news_details | a:1:{s:10:isFeatured;s:1:1;}

这就是我的代码的外观:

function add_news_details_meta_box() {
    add_meta_box(
        'news_details_meta_box', // $id
        'Checkbox', // $title
        'show_news_details_meta_box', // $callback
        'news', // $screen
        'normal', // $context
        'high' // $priority
    );
}
add_action( 'add_meta_boxes', 'add_news_details_meta_box' );

function show_news_details_meta_box() {
    global $post;
        $meta = get_post_meta( $post->ID, 'news_details', true ); ?>

  <input type="hidden" name="news_details_meta_box_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>">

    <p>
    <label for="news_details[isFeatured]">Is this post featured?
      <input type="checkbox" name="news_details[isFeatured]" value="1" <?php if (is_array($meta) && $meta['isFeatured'] === '1' ) { echo 'checked';} else { echo 'Unchecked';}?>>
    </label>
  </p>

  <?php
}
function save_news_details_meta( $post_id ) {
    // verify nonce
    if ( !wp_verify_nonce( $_POST['news_details_meta_box_nonce'], basename(__FILE__) ) ) {
        return $post_id;
    }
    // check autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }
    // check permissions
    if ( 'page' === $_POST['post_type'] ) {
        if ( !current_user_can( 'edit_page', $post_id ) ) {
            return $post_id;
        } elseif ( !current_user_can( 'edit_post', $post_id ) ) {
            return $post_id;
        }
    }

    $old = get_post_meta( $post_id, 'news_details', true );
    $new = $_POST['news_details'];
    if ( $new && $new !== $old ) {
        update_post_meta( $post_id, 'news_details', $new );
    } elseif ( '' === $new && $old ) {
        delete_post_meta( $post_id, 'news_details', $old );
    }
}
add_action( 'save_post', 'save_news_details_meta' ); 

你能给我解释一下,过一段时间更新复选框有什么问题吗

我想你应该检查一下if条件。按照我获得您描述的方式,$meta['isFeatured']='1'总是成为现实。在输入标记中将该值设置为1。因此,保存值1将保存到字段中。在下一次加载时,您将使用if检查$meta['isFeatured']是否为1,因此这始终是真的

您可以检查表单中的复选框是否选中了:

isset($_POST['name_of_your_checkbox_input'])

如果已设置,则保存值1。如果不是,则保持预定义的值0。

在本例中对我有效的是更改}elseif===$new&&$old{to}否则===$new&&$old{