如果选中复选框,则显示元数据

时间:2017-06-26 作者:Wagner Moreira

因此,我的自定义帖子类型在帖子编辑器上具有以下结构:

features_metabox:
  - option1 []
  - option2 []
  - option3 []


metabox_opt1
 -field1

metabox_opt2
 -field2

metabox_opt3
 -field3
我想要的是在features\\u metabox中标记op1复选框时显示metabox\\u op1,我知道如何使用javascript,但我在WP中,我不知道如何使用php,以前有人这样做过吗?

1 个回复
最合适的回答,由SO网友:dbeja 整理而成

为什么不只使用一个metabox,然后使用jQuery管理要显示的选项:

// add metabox
function adding_custom_meta_boxes( $post_type, $post ) {
    add_meta_box(
        \'my-meta-box\',
        __( \'My Meta Box\' ),
        \'render_my_meta_box\',
        \'post\',
        \'normal\',
        \'default\'
    );
}
add_action( \'add_meta_boxes\', \'adding_custom_meta_boxes\', 10, 2 );

// render metabox
function render_my_meta_box( $post ) {
    wp_nonce_field( \'my_metas_nonce\', \'my_metas_nonce\' );

    $my_features_1 = get_post_meta( $post->ID, \'my_features_1\', true );
    $my_option_1 = get_post_meta( $post->ID, \'my_option_1\', true );
    $my_features_2 = get_post_meta( $post->ID, \'my_features_2\', true );
    $my_option_2 = get_post_meta( $post->ID, \'my_option_2\', true );
    $my_features_3 = get_post_meta( $post->ID, \'my_features_3\', true );
    $my_option_3 = get_post_meta( $post->ID, \'my_option_3\', true );

    ?>
        <input type="checkbox" name="my_features_1" class="my_features" value="my_option_1" <?php checked($my_features_1, \'my_option_1\'); ?>>Option 1<br>
        <input type="checkbox" name="my_features_2" class="my_features" value="my_option_2" <?php checked($my_features_2, \'my_option_2\'); ?>>Option 2<br>
        <input type="checkbox" name="my_features_3" class="my_features" value="my_option_3" <?php checked($my_features_3, \'my_option_3\'); ?>>Option 3<br>

        <div id="my_option_1" class="my_options" style="<?php if( ! $my_features_1 ) echo \'display: none;\'; ?>">
            <h3>Option 1</h3>
            <input name="my_option_1" value="<?php echo $my_option_1; ?>" />
        </div>

        <div id="my_option_2" class="my_options" style="<?php if( ! $my_features_2 ) echo \'display: none;\'; ?>">
            <h3>Option 2</h3>
            <input name="my_option_2" value="<?php echo $my_option_2; ?>" />
        </div>

        <div id="my_option_3" class="my_options" style="<?php if( ! $my_features_3 ) echo \'display: none;\'; ?>">
            <h3>Option 3</h3>
            <input name="my_option_3" value="<?php echo $my_option_3; ?>" />
        </div>

        <script>
            jQuery(document).ready(function($) {
                $(\'.my_features\').change(function() {
                    var checkbox = $(this);
                    if( checkbox.is(\':checked\') ) {
                        $( \'#\' + checkbox.val() ).show();
                    } else {
                        $( \'#\' + checkbox.val() ).hide();
                    }
                });
            });
        </script>
    <?php
}

// save metabox data
function save_post_features_meta( $post_id ){
    if ( ! isset( $_POST[\'my_metas_nonce\'] ) || ! wp_verify_nonce( $_POST[\'my_metas_nonce\'], \'my_metas_nonce\' ) || ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) || ! current_user_can( \'edit_post\', $post_id ) ) {
        return;
    }

    update_post_meta( $post_id, \'my_features_1\', sanitize_text_field($_POST[\'my_features_1\']));
    update_post_meta( $post_id, \'my_option_1\', sanitize_text_field($_POST[\'my_option_1\']));

    update_post_meta( $post_id, \'my_features_2\', sanitize_text_field($_POST[\'my_features_2\']));
    update_post_meta( $post_id, \'my_option_2\', sanitize_text_field($_POST[\'my_option_2\']));

    update_post_meta( $post_id, \'my_features_3\', sanitize_text_field($_POST[\'my_features_3\']));
    update_post_meta( $post_id, \'my_option_3\', sanitize_text_field($_POST[\'my_option_3\']));
}
add_action( \'save_post\', \'save_post_features_meta\');

结束

相关推荐

在导航设置中添加自定义菜单项Metabox

我一直在设想一种功能,可以扩展WordPress拖放菜单的创建。本质上是添加一个自定义菜单项按钮,用于添加不同类型的项目,而不仅仅是链接。我设想一个基本用法只是一个文本字段,但未来的应用程序可能包括在菜单项中添加小部件区域。Visual Mockup of Idea:如果有任何信息和资源可以帮助我找到可能的解决方案,我将不胜感激。