【问题标题】:If-else statement for checkbox in wordpress widget won't workwordpress 小部件中复选框的 if-else 语句将不起作用
【发布时间】:2015-03-22 19:47:17
【问题描述】:

这是我在插件中的小部件代码:

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 通知。

我尝试使用一个 = 并且不使用引号,但没有任何帮助。有什么帮助吗?

【问题讨论】:

    标签: php wordpress if-statement checkbox widget


    【解决方案1】:

    通过深入了解 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
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 2023-03-13
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      相关资源
      最近更新 更多