POST Metabox的多选框

时间:2011-01-07 作者:idontknowhow

嗨,伙计们,我对此很头疼,我对本教程wpshout进行了多检查。com/在wordpress中创建一个in-post主题选项元框/问题是它没有保存所有选中的值,并且在编辑帖子时也没有选中选中的值。

这是我的密码

我在这里为muticheckbox添加了一个数组

    <?php    function hybrid_post_meta_boxes() {

/* Array of the meta box options. */
$meta_boxes = array(
    \'title\' => array( \'name\' => \'Title\', \'title\' => __(\'Title\', \'hybrid\'), \'type\' => \'text\' ),
    \'description\' => array( \'name\' => \'Description\', \'title\' => __(\'Description\', \'hybrid\'), \'type\' => \'textarea\' ),
    \'location\' => array( \'name\' => \'location\', \'title\' => __(\'Location:\', \'hybrid\'), \'type\' => \'check\', \'options\' => array(
    \'AL\' => \'Alabama\',
    \'CA\' => \'California\',
    \'DE\' => \'Delaware\',
),
);

return apply_filters( \'hybrid_post_meta_boxes\', $meta_boxes ); } ?>
我为多重复选框添加else语句

    <?php
    function post_meta_boxes() {
    global $post;
    $meta_boxes = hybrid_post_meta_boxes(); ?>

<table class="form-table">
<?php foreach ( $meta_boxes as $meta ) :

    $value = get_post_meta( $post->ID, $meta[\'name\'], true );

    if ( $meta[\'type\'] == \'text\' )
        get_meta_text_input( $meta, $value );
    elseif ( $meta[\'type\'] == \'textarea\' )
        get_meta_textarea( $meta, $value );
    elseif ( $meta[\'type\'] == \'select\' )
        get_meta_select( $meta, $value );
    elseif ( $meta[\'type\'] == \'check\' )
        get_meta_check( $meta, $value );

endforeach; ?>
</table>

我为multicheck创建了一个函数

    <?php function get_meta_check( $args = array(), $value = false ) {

extract( $args ); ?>

<tr>
    <th style="width:10%;">
        <label for="<?php echo $name; ?>"><?php echo $title; ?></label>
    </th>
    <td>

        <?php foreach ( $options as $option ) : ?>
          <input type="checkbox" name="<?php echo $name; ?>" id="<?php echo $name; ?>" value=", <?php echo $option; ?>, " <?php if ( htmlentities( $value, ENT_QUOTES ) == $option ) echo \' checked="checked"\'; ?> /> <?php echo $option; ?>,
        <?php endforeach; ?>
        <input type="hidden" name="<?php echo $name; ?>_noncename" id="<?php echo $name; ?>_noncename" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
    </td>
</tr>
<?php } ?>
如何编写代码来保存复选框的所有值?谢谢

使现代化

    function save_meta_data( $post_id ) {
    global $post;

    if ( \'page\' == $_POST[\'post_type\'] )
        $meta_boxes = array_merge( page_meta_boxes() );
    else
        $meta_boxes = array_merge( post_meta_boxes() );

    foreach ( $meta_boxes as $meta_box ) :

        if ( !wp_verify_nonce( $_POST[$meta_box[\'name\'] . \'_noncename\'], plugin_basename( __FILE__ ) ) )
            return $post_id;

        if ( \'page\' == $_POST[\'post_type\'] && !current_user_can( \'edit_page\', $post_id ) )
            return $post_id;

        elseif ( \'post\' == $_POST[\'post_type\'] && !current_user_can( \'edit_post\', $post_id ) )
            return $post_id;

        $data = stripslashes( $_POST[$meta_box[\'name\']] );
        if ( get_post_meta( $post_id, $meta_box[\'name\'] ) == \'\' )
            add_post_meta( $post_id, $meta_box[\'name\'], $data, true );

        elseif ( $data != get_post_meta( $post_id, $meta_box[\'name\'], true ) )
            update_post_meta( $post_id, $meta_box[\'name\'], $data );

        elseif ( $data == \'\' )
            delete_post_meta( $post_id, $meta_box[\'name\'], get_post_meta( $post_id, $meta_box[\'name\'], true ) );

    endforeach;
}    ?>

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

我拿走了the example code you suggested 并更改了以下内容以获得multicheck支持:

// Add this at the end of the file
// A good prefix for your name prevents problems later!
function wpse6513_get_meta_check( $args = array(), $current_values = array() )
{
    extract( $args );

    $name_esc = esc_attr( $name );

    if ( ! is_array( $current_values ) ) {
        $current_values = array();
    }

    echo \'<tr>\';
    echo \'<th style="width: 10%">\';
    echo \'<label for="\' . $name_esc . \'">\' . esc_html( $title ) . \'</label>\';
    echo \'</th>\';
    echo \'<td>\';

    foreach ( $options as $option_value => $option_label ) {
        $option_value_esc = esc_attr( $option_value );
        echo \'<label><input type="checkbox" name="\' . $name_esc . \'[]" id="\' . $name_esc . \'_\' . $option_value_esc . \'" value="\' . $option_value_esc . \'"\';
        if ( in_array( $option_value, $current_values ) ) {
            echo \' checked="checked"\';
        }
        echo \'/> \' . esc_html( $option_label );
        echo \'</label><br/>\';
    }
    echo \'<input type="hidden" name="\' . $name_esc . \'_noncename" id="\' . $name_esc . \'_noncename" value="\' . wp_create_nonce( plugin_basename( __FILE__ ) ) . \'" />\';

    echo \'</td>\';
    echo \'</tr>\';
}
在中hybrid_save_meta_data():

// Replace this line:
$data = stripslashes( $_POST[$meta_box[\'name\']] );
// By these lines:
$data = array_key_exists( $meta_box[\'name\'], $_POST ) ? $_POST[$meta_box[\'name\']] : \'\';
if ( is_array( $data ) ) {
    $data = array_map( \'stripslashes\', $data );
} else {
    $data = stripslashes( $data );
}
当然,在post_meta_boxes():

// Add this to the `$meta[\'type\']` if/else list
elseif ( $meta[\'type\'] == \'check\' )
    wpse6513_get_meta_check( $meta, $value );
现在,您可以处理您建议的表单的“多检查”:

\'location\' => array(
    \'name\' => \'location\',
    \'title\' => __(\'Location:\', \'hybrid\'),
    \'type\' => \'check\',
    \'options\' => array(
        \'AL\' => \'Alabama\',
        \'CA\' => \'California\',
        \'DE\' => \'Delaware\',
    ),
),
最后是一些防止警告的代码(除非启用WP_DEBUG):

// Replace all `wp_specialchar()` calls with `esc_attr()`
// Add these lines in `hybrid_save_meta_data()`:
// At the start, after `global $post`:
if ( ! array_key_exists( \'post_type\', $_POST ) ) {
    return $post_id;
}

// In the foreach loop, instead of:
if ( !wp_verify_nonce( $_POST[$meta_box[\'name\'] . \'_noncename\'], plugin_basename( __FILE__ ) ) )
    return $post_id;
// Write:
$nonce_name = $meta_box[\'name\'] . \'_noncename\';
if ( ! array_key_exists( $nonce_name, $_POST ) || !wp_verify_nonce( $_POST[$nonce_name], plugin_basename( __FILE__ ) ) )
    return $post_id;

结束

相关推荐

自定义Query_Posts()参数

我正在为主题添加投票功能。访问者可以对帖子投赞成票或反对票。我创建了一个表来存储每个帖子的投票数,效果很好。现在,我正试图按投票对帖子进行排序。我有“投赞成票”和“投反对票”的链接。例如,当您单击“投票支持”一个新参数时sort=up 在URL中传递。在循环中,如果参数存在并且为=“up”,则我希望使用投票向上循环帖子。wpdb->get_results(\"SELECT post_id, FROM $wpdb->votes WHERE up > 5\"); 这就是我想用的东西