wordpress 小部件中复选框的 If-else 语句将不起作用

If-else statement for checkbox in wordpress widget won't work

这是我的插件插件代码:

class shortcodes_widget extends WP_Widget {
function shortcodes_widget() {
    parent::WP_Widget(false, $name = __('t', 't') );
}

function form($instance) {
if($instance) {
 $title = esc_attr($instance['title']);
 $textarea = esc_textarea($instance['textarea']);
 $checkbox = esc_attr($instance['checkbox']);
} else {
 $title = '';
 $textarea = '';
 $checkbox = '';
}
?>

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

<p>
  <label for="<?php echo $this->get_field_id('textarea'); ?>"><?php _e('t', 't'); ?></label>
  <textarea class="widefat" id="<?php echo     $this->get_field_id('textarea'); ?>" name="<?php echo  $this->get_field_name('textarea'); ?>"><?php echo $textarea; ?></textarea>
</p>
  <p>
  <input id="<?php echo $this->get_field_id('checkbox'); ?>"  name="<?php   echo $this->get_field_name('checkbox'); ?>" type="checkbox" value="1" />
   <label for="<?php echo $this->get_field_id('checkbox'); ?>"><?php _e('t', 't'); ?></label>
 </p>

 <?php
 }

   function update($new_instance, $old_instance) {
  $instance = $old_instance;
  $instance['title'] = strip_tags($new_instance['title']);
  $instance['textarea'] = strip_tags($new_instance['textarea']);
   $instance['checkbox'] = strip_tags($new_instance['checkbox']);

 return $instance;
 }

 public function widget( $args, $instance ) {
 extract( $args );
 while(isset($textarea)) { 
 $textarea = $instance['textarea'];  
  $checkbox = $instance['checkbox'];  
 }

   if(isset($checkbox) and $checkbox == '1') { 
   echo '<div class="myclass">'; 

 }
else { 
 echo '<div>';
}

 echo $before_widget;
if (isset($instance['textarea'])) { 
echo do_shortcode($instance['textarea']);   
} 
echo $after_widget;
echo '</div>';
 }
 }
   add_action('widgets_init', create_function('', 'return           register_widget("shortcodes_widget");'));

问题是这样的:

if(isset($checkbox) and $checkbox == '1') { 
     echo '<div class="myclass">';   
   }
  else { 
   echo '<div>';
  }

如果复选框被选中,它不会回显第一个值(div 和 "myclass")。仅回应 "else" 的第二个值。我必须添加 isset 和 while 函数以避免 PHP 通知。

我试过使用一个 = 而没有引号,但没有任何帮助。有什么帮助吗?

通过深入了解 WordPress 核心来学习并获得灵感!以下是默认 WP 小部件如何处理控制下拉选项的复选框:

    public function widget( $args, $instance ) {
        $d = ! empty( $instance['dropdown'] ) ? '1' : '0';
        if ( $d ) {
            // do stuff
        }
    }
    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['dropdown'] = ! empty( $new_instance['dropdown'] ) ? 1 : 0;
        return $instance;
    }
    public function form( $instance ) {
        $dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
?>
        <p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>"<?php checked( $dropdown ); ?> />
        <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Display as dropdown' ); ?></label><br />
<?php
    }