扩展WP_Widget复选框Not Work

时间:2017-08-29 22:02:36

标签: php wordpress widget

我的小部件插件不起作用! 我使用两个ceheckbox和不同的名称和ID,但它不起作用,不保存和更新。

请在下面说出我的代码:

function update( $new_instance, $old_instance ) {
    $instance               = $old_instance;
    $instance['title']      = strip_tags($new_instance['title']);
    $instance['number']     = (int) $new_instance['number'];
    $instance['show_date']  = (isset($new_instance['show_date']) AND $new_instance['show_date'] == 1) ? 1 : 0;
    $instance['show_img']   = (isset($new_instance['show_img']) AND $new_instance['show_img'] == 1) ? 1 : 0;

    return $instance;
}

function form( $instance ) {
    $title      = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
    $number     = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;     
    $show_date  = (isset($instance['show_date']) AND $instance['show_date'] == 1) ? 1 : 0;
    $show_img   = (isset($instance['show_img']) AND $instance['show_img'] == 1) ? 1 : 0;

    $wp_theme = wp_get_theme();
    $wp_theme = $wp_theme->get('TextDomain');

    ?>

    <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></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( 'number' ); ?>"><?php _e( 'Number of posts to display:' ); ?></label>
    <input class="widefat" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>

    <p><input id="<?php echo ( $this->get_field_id('show_img') ); ?>" name="<?php echo ( $this->get_field_name('show_img') ); ?>" type="checkbox" value="1" <?php if($show_img) echo ' checked="checked"'; ?> />
    <label for="<?php echo ( $this->get_field_id('show_img') ); ?>"><?php _e( 'Display feature image?' ); ?></label></p>

    <p><input id="<?php echo ( $this->get_field_id('show_date') ); ?>" name="<?php echo ( $this->get_field_name('show_date') ); ?>" type="checkbox" value="1" <?php if($show_date) echo ' checked="checked"'; ?> />
    <label for="<?php echo ( $this->get_field_id('show_date') ); ?>"><?php _e( 'Display post date?' ); ?></label></p>

    <?php
}

并访问LINK以获取完整代码谢谢

0 个答案:

没有答案