首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WP Metabox不保存后期元数据

WP Metabox不保存后期元数据
EN

Stack Overflow用户
提问于 2019-05-14 19:05:53
回答 1查看 130关注 0票数 0

我正在尝试在页面上实现一个简单的复选框,以便动态添加一个超文本标记语言块,以防用户选择它,但我无法保存post_meta来执行此任务,有人能帮助我吗?此复选框输入的值不会保存在帖子元信息中。

这是我到目前为止在我的functions.php上得到的

代码语言:javascript
复制
function wporg_add_custom_box(){
    $screens = ['page', 'wporg_cpt'];
    foreach ($screens as $screen) {
        add_meta_box(
            'wporg_box_id',           // Unique ID
            'Entra in Flee Block',  // Box title
            'wporg_custom_box_html',  // Content callback, must be of type callable
            $screen,                   // Post type
            'side'
        );
    }
}
add_action('add_meta_boxes', 'wporg_add_custom_box');


function wporg_custom_box_html($post){
    $value = get_post_meta($post->ID, '_wporg_meta_key', true);
    ?>
    <label for="wporg_field">Add "Entra in Flee" block to page</label>
    </br>
    <input type="checkbox" name="wporg_field" id="wporg_field" class="postbox">
    <?php
}


function wporg_save_postdata($post_id){
    if (array_key_exists('wporg_field', $_POST)) {
        update_post_meta(
            $post_id,
            '_wporg_meta_key',
            $_POST['wporg_field']
        );
    }
}
add_action('save_post', 'wporg_save_postdata');
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-14 20:28:40

您没有在wp_cusotm_box_html函数上使用$value。我认为应该是这样的:

代码语言:javascript
复制
function wporg_add_custom_box()
{
    $screens = ['page', 'wporg_cpt'];
    foreach ($screens as $screen) {
        add_meta_box(
            'wporg_box_id',           // Unique ID
            'Entra in Flee Block',  // Box title
            'wporg_custom_box_html',  // Content callback, must be of type callable
            $screen,                   // Post type
            'side'
        );
    }
}

add_action('add_meta_boxes', 'wporg_add_custom_box');
function wporg_custom_box_html($post)
{
    $value = get_post_meta($post->ID, '_wporg_meta_key', true) ? 'checked' : '';
    ?>
    <label for="wporg_field">Add "Entra in Flee" block to page</label>
    </br>
    <input type="checkbox" name="wporg_field" id="wporg_field" class="postbox" <?php echo $value; ?>>
    <?php
}

function wporg_save_postdata($post_id)
{
    if (array_key_exists('wporg_field', $_POST)) {
        update_post_meta(
            $post_id,
            '_wporg_meta_key',
            $_POST['wporg_field']
        );
    } else {
        delete_post_meta(
            $post_id,
            '_wporg_meta_key'
        );
    }
}

add_action('save_post', 'wporg_save_postdata');
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56128979

复制
相关文章

相似问题

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