如何在wordpress小部件窗体中添加单选按钮

如何在wordpress小部件窗体中添加单选按钮,wordpress,Wordpress,如何在wordpress小部件窗体中添加单选按钮?我已经能够添加得到保存和工作良好的输入字段。但是我在单选按钮方面有问题。有人吗 这是我输入字段的代码: <p><label for="<?php echo $this->get_field_id('id'); ?>"><?php _e('Video ID:'); ?> <input class="widefat" id="<?php echo $this->get_

如何在wordpress小部件窗体中添加单选按钮?我已经能够添加得到保存和工作良好的输入字段。但是我在单选按钮方面有问题。有人吗

这是我输入字段的代码:

<p><label for="<?php echo $this->get_field_id('id'); ?>"><?php _e('Video ID:'); ?> 
    <input class="widefat" id="<?php echo $this->get_field_id('id');  ?>" 
    name="<?php echo $this->get_field_name('id'); ?>" 
    type="text" value="<?php echo $id; ?>" /></label></p>

创建每个单选按钮,并确保它们位于同一“组”下。下面是您想要的基本HTML版本

<input type="radio" name="group1" value="small" checked>Small<br />
<input type="radio" name="group1" value="full"> Full<br />

我尝试使用此选项,但它无法保存我的选择。。 我想我不明白这个脚本中var$大小是从哪里来的

我的问题是单选按钮无法保持选中状态。。我试着用 if/else解决方案。。但只有一个选项需要检查。。 (我知道从逻辑上讲这很奇怪)

我的代码是我正在构建的小部件(wp小部件)的一部分

这是我的密码:

        <!-- Your Name: Text Input -->
    <?php if( $visiblecategories == showempty ) { ?>
    <p>
        <label for="<?php echo $this->get_field_id( 'visiblecategories' ); ?>"><?php _e('Show Empty Category:', 'badcategory'); ?></label><br />
        <input type="radio" id="<?php echo $this->get_field_id( 'visiblecategories' ); ?>" name="<?php echo $this->get_field_name( 'visiblecategories' ); ?>" value="showempty" style="" checked /> Show Empty<br />
        <input type="radio" id="<?php echo $this->get_field_id( 'visiblecategories' ); ?>" name="<?php echo $this->get_field_name( 'visiblecategories' ); ?>" value="hideempty" style="" /> Hide Empty<br />            
    </p>
    <?php } else { ?>
    <?php echo $visiblecategories; ?>
    <p>
        <label for="<?php echo $this->get_field_id( 'visiblecategories' ); ?>"><?php _e('Show Empty Category:', 'badcategory'); ?></label><br />
        <input type="radio" id="<?php echo $this->get_field_id( 'visiblecategories' ); ?>" name="<?php echo $this->get_field_name( 'visiblecategories' ); ?>" value="showempty" style="" /> Show Empty<br />
        <input type="radio" id="<?php echo $this->get_field_id( 'visiblecategories' ); ?>" name="<?php echo $this->get_field_name( 'visiblecategories' ); ?>" value="hideempty" style="" checked /> Hide Empty<br />            
    </p>
    <?php } ?>



对属于同一组的所有单选按钮使用相同的名称,并使用该名称访问值

例如,如果您有一个名为“gender”的无线组,其值为“male”和“female”,则对除get_field_name之外的所有get_field*调用使用male/female。使用“性别”作为get_字段的名称


在您的widget()和update()代码中,仅使用“性别”-这将有男性或女性作为值。

对于任何与javascript问题斗争的人,错误检查得到检查,使舒尔HTML不具有空的
检查=“”
属性。仅在选定单选按钮上添加属性

此代码适用于带有单选按钮的小部件,不带if语句:

function form( $instance ) {

    /* Option carrier is 'ecw_column' */
    $ecw_column = isset( $instance['ecw_column'] ) ? $instance['ecw_column'] : 'ecw_column_none';

    echo '<p>';

    $value = 'ecw_column_1';

    echo '<input value="'. $value .'" class="widefat" id="'. $this->get_field_id($value) .'" name="'. $this->get_field_name('ecw_column') .'" type="radio"'. ($ecw_column == $value ? ' checked="checked"' : '') .' />';
    echo '<label for="'. $this->get_field_id($value) .'">'. __('Column start') .'</label>';
    echo '<br/>';

    $value = 'ecw_column_2';

    echo '<input value="'. $value .'" class="widefat" id="'. $this->get_field_id($value) .'" name="'. $this->get_field_name('ecw_column') .'" type="radio"'. ($ecw_column == $value ? ' checked="checked"' : '') .' />';
    echo '<label for="'. $this->get_field_id($value) .'">'. __('Breakpoint') .'</label>';
    echo '<br/>';

    $value = 'ecw_column_3';

    echo '<input value="'. $value .'" class="widefat" id="'. $this->get_field_id($value) .'" name="'. $this->get_field_name('ecw_column') .'" type="radio"'. ($ecw_column == $value ? ' checked="checked"' : '') .' />';
    echo '<label for="'. $this->get_field_id($value) .'">'. __('Column end') .'</label>';
    echo '<br/>';

    /* Default value */
    $value = 'ecw_column_none';

    echo '<input value="'. $value .'" class="widefat" id="'. $this->get_field_id($value) .'" name="'. $this->get_field_name('ecw_column') .'" type="radio"'. ($ecw_column == $value ? ' checked="checked"' : '') .' />';
    echo '<label for="'. $this->get_field_id($value) .'">'. __('Current') .'</label>';

    echo '</p>';
}
函数形式($instance){
/*选项载体为“ecw_列”*/
$ecw_column=isset($instance['ecw_column'])?$instance['ecw_column']:'ecw_column\u none';
回声“”;
$value='ecw_column_1';
回声';
回显“”。_('列开始')。';
回声“
”; $value='ecw_column_2'; 回声'; 回显“.”(“断点”); 回声“
”; $value='ecw_column_3'; 回声'; 回显“”。_u('列结束')。'; 回声“
”; /*默认值*/ $value='ecw_column_none'; 回声'; 回显“”。_u('当前')。'; 回声“

”; }
谢谢,太好了。不过我无法将其保存到数据库中,有什么想法吗?嗯。。在我的示例中,所选内容将存储在to$_POST['group1']中,因此您可以尝试检查该变量。另请看一个PHP表单处理单选按钮选择的真实示例:
        <!-- Your Name: Text Input -->
    <?php if( $visiblecategories == showempty ) { ?>
    <p>
        <label for="<?php echo $this->get_field_id( 'visiblecategories' ); ?>"><?php _e('Show Empty Category:', 'badcategory'); ?></label><br />
        <input type="radio" id="<?php echo $this->get_field_id( 'visiblecategories' ); ?>" name="<?php echo $this->get_field_name( 'visiblecategories' ); ?>" value="showempty" style="" checked /> Show Empty<br />
        <input type="radio" id="<?php echo $this->get_field_id( 'visiblecategories' ); ?>" name="<?php echo $this->get_field_name( 'visiblecategories' ); ?>" value="hideempty" style="" /> Hide Empty<br />            
    </p>
    <?php } else { ?>
    <?php echo $visiblecategories; ?>
    <p>
        <label for="<?php echo $this->get_field_id( 'visiblecategories' ); ?>"><?php _e('Show Empty Category:', 'badcategory'); ?></label><br />
        <input type="radio" id="<?php echo $this->get_field_id( 'visiblecategories' ); ?>" name="<?php echo $this->get_field_name( 'visiblecategories' ); ?>" value="showempty" style="" /> Show Empty<br />
        <input type="radio" id="<?php echo $this->get_field_id( 'visiblecategories' ); ?>" name="<?php echo $this->get_field_name( 'visiblecategories' ); ?>" value="hideempty" style="" checked /> Hide Empty<br />            
    </p>
    <?php } ?>
function form( $instance ) {

    /* Option carrier is 'ecw_column' */
    $ecw_column = isset( $instance['ecw_column'] ) ? $instance['ecw_column'] : 'ecw_column_none';

    echo '<p>';

    $value = 'ecw_column_1';

    echo '<input value="'. $value .'" class="widefat" id="'. $this->get_field_id($value) .'" name="'. $this->get_field_name('ecw_column') .'" type="radio"'. ($ecw_column == $value ? ' checked="checked"' : '') .' />';
    echo '<label for="'. $this->get_field_id($value) .'">'. __('Column start') .'</label>';
    echo '<br/>';

    $value = 'ecw_column_2';

    echo '<input value="'. $value .'" class="widefat" id="'. $this->get_field_id($value) .'" name="'. $this->get_field_name('ecw_column') .'" type="radio"'. ($ecw_column == $value ? ' checked="checked"' : '') .' />';
    echo '<label for="'. $this->get_field_id($value) .'">'. __('Breakpoint') .'</label>';
    echo '<br/>';

    $value = 'ecw_column_3';

    echo '<input value="'. $value .'" class="widefat" id="'. $this->get_field_id($value) .'" name="'. $this->get_field_name('ecw_column') .'" type="radio"'. ($ecw_column == $value ? ' checked="checked"' : '') .' />';
    echo '<label for="'. $this->get_field_id($value) .'">'. __('Column end') .'</label>';
    echo '<br/>';

    /* Default value */
    $value = 'ecw_column_none';

    echo '<input value="'. $value .'" class="widefat" id="'. $this->get_field_id($value) .'" name="'. $this->get_field_name('ecw_column') .'" type="radio"'. ($ecw_column == $value ? ' checked="checked"' : '') .' />';
    echo '<label for="'. $this->get_field_id($value) .'">'. __('Current') .'</label>';

    echo '</p>';
}