小工具未保存的单选按钮

时间:2017-08-21 21:02:31

标签: wordpress radio-button widget radio

如何保存当前的“就业状态”(表单功能)并在前端(小部件功能)中显示?

// Create the widget output.
public function widget( $args, $instance ) {
  $title = apply_filters( 'widget_title', $instance[ 'title' ] );
  $employment = apply_filters( 'widget_title', $instance[ 'employment' ] );
  echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; ?>
  <p>Current state of employment: <?php echo $employment ?></p>
  <?php echo $args['after_widget'];
}


// Create the admin area widget settings form.
public function form( $instance ) {
  $title = ! empty( $instance['title'] ) ? $instance['title'] : ''; 
  $employment = ! empty( $instance['employment'] ) ? $instance['employment'] : 'help'; 
  ?>
  <p>
  <label>  Current State of employment:</label><br>
  <input for="<?php echo $this->get_field_id( 'employment' ); ?>" type="radio" name="employment" value="employed"> Employed<br>
  <input for="<?php echo $this->get_field_id( 'employment' ); ?>" type="radio" name="employment" value="unemployed"> Unemployed<br>
</p>
<?php
}

1 个答案:

答案 0 :(得分:1)

试试此代码

<input type="radio" <?php  checked($instance[ 'employment' ] ,'employment'); ?> id="<?php echo $this->get_field_id( 'employment' ); ?>" value="employment" name="<?php echo $this->get_field_name( 'employment' ); ?>" ><br>

<input type="radio" <?php  checked($instance[ 'employment' ] ,'unemployed'); ?> id="<?php echo $this->get_field_id( 'unemployed' ); ?>" value="unemployed" name="<?php echo $this->get_field_name( 'employment' ); ?>" >