在WordPress中,为什么我的自定义小部件在重新加载后不会保留在侧边栏中?

时间:2014-01-12 04:22:45

标签: wordpress widget

将自定义窗口小部件拖动到窗口小部件区域并重新加载页面后,它就消失了。有什么想法吗?

<?php
    add_action('widgets_init','register_postTypeWidget');

    function register_postTypeWidget(){
        register_widget('postType_widget');
    }

    class postType_widget extends WP_Widget {
        function postType_widget() {  
            $widget_ops = array( 'classname' => 'postType_widget', 'description' => __('Display Posts by Post Type') );  
            $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'postType_widget' );  
            $this->WP_Widget( 'postType_widget', __('Posts by Post Type', 'postType_widget'), $widget_ops, $control_ops );  
        }
         function widget($args, $instance) {
            extract($args); 
            echo "hello world";

        }
        function update( $new_instance, $old_instance ) {  

            $instance = $old_instance;

            $instance['title'] = strip_tags( $new_instance['title'] );
            $instance['postTypeSelect'] = strip_tags( $new_instance['postTypeSelect'] );
            $instance['postTypeNumber'] = strip_tags( $new_instance['postTypeNumber'] );
            $instance['selectedPostTag'] = strip_tags( $new_instance['selectedPostTag'] );
            $instance['showDate'] = strip_tags( $new_instance['showDate'] );
            $instance['showAuthor'] = strip_tags( $new_instance['showAuthor'] );

            return $instance;

        }
        function form( $instance ) {    
        $defaults = array( 'title' => __(''), 'postTypeSelect' => __(''), 'postType_number' =>  __(''), 'selected_postTag' =>  __(''), 'show_date' =>  __(''), 'show_author' =>  __('') );
        $instance = wp_parse_args( (array) $instance, $defaults);   

        $args = '';
        $output = 'object';
        $post_types = get_post_types( $args, $output );
        $posttags = get_tags();
    ?>
            <p>
                <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title'); ?></label>
                <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'postTypeSelect' ); ?>"><?php _e('Select Post Type'); ?></label>
                <?php 
                    echo '<select name="postTypeSelect" style="width:100%;">';
                    echo '<option value="">Select Post Type</option>';
                    foreach($post_types as $post_type) {
                        if($instance['postTypeSelect'] == $post_type->name)
                            echo '<option value="'.$post_type->name.'" selected >'.$post_type->label.'</option>';
                        else 
                            echo '<option value="'.$post_type->name.'" >'.$post_type->label.'</option>';
                    }
                    echo '</select>';
                ?>
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'postTypeNumber' ); ?>"><?php _e('Number of Posts'); ?></label>
                <?php 
                    echo '<select name="postTypeNumber" style="width:100%;">';
                    echo '<option value="">Number of Posts</option>';
                    for($x=1; $x < 11; $x++) {
                        if($instance['postTypeNumber'] == $x)
                            echo '<option value="'.$x.'" selected >'.$x.'</option>';
                        else
                            echo '<option value="'.$x.'" >'.$x.'</option>';
                    }
                    echo '</select>';
                ?>
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'selectedPostTag' ); ?>" style="display:block;width:100%;"><?php _e('Show Only Posts with These Tags (optional)'); ?></label>
                <?php
                    foreach($posttags as $postTag){
                        if($instance['selected_postTag'] == $postTag->term_id)
                            echo '<input type="checkbox" name="selectedPostTag[]" value="'.$postTag->term_id.'" checked />'.$postTag->name.'</br>';
                        else
                            echo '<input type="checkbox" name="selectedPostTag[]" value="'.$postTag->term_id.'" />'.$postTag->name.'</br>';
                    }
                ?>
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'additional_options' ); ?>" style="display:block;width:100%;"><?php _e('Additional Options'); ?></label>
                <?php if($instance['show_date'] == 'show_date'): ?>
                    <input type="checkbox" id="<?php echo $this->get_field_id( 'showDate' ); ?>" name="<?php echo $this->get_field_name( 'showDate' ); ?>" value="showDate" checked />Show Date</br>
                <?php else: ?>
                    <input type="checkbox" id="<?php echo $this->get_field_id( 'showDate' ); ?>" name="<?php echo $this->get_field_name( 'showDate' ); ?>" value="showDate" />Show Date</br>
                <?php endif; ?>

                <?php if($instance['show_author'] == 'show_author'): ?>
                    <input type="checkbox" id="<?php echo $this->get_field_id( 'showAuthor' ); ?>" name="<?php echo $this->get_field_name( 'showAuthor' ); ?>" value="showAuthor" checked />Show Author</br>
                <?php else: ?>
                    <input type="checkbox" id="<?php echo $this->get_field_id( 'showAuthor' ); ?>" name="<?php echo $this->get_field_name( 'showAuthor' ); ?>" value="showAuthor" />Show Author</br>
                <?php endif; ?>
            </p>
    <?php
        }
    }
?>

1 个答案:

答案 0 :(得分:0)

我认为您的窗口小部件的id_base值必须小写。查看WP_Widget的来源:

/**
 * PHP5 constructor
 *
 * @param string $id_base Optional Base ID for the widget, lower case,
 * if left empty a portion of the widget's class name will be used. Has to be unique.
 <snip>
 */
function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {

所以这样的东西应该在你的构造函数中起作用(注意我也更改了$id_base$control_ops的大小写):

function postType_widget() {  
    $widget_ops = array( 'classname' => 'postType_widget', 'description' => __('Display Posts by Post Type') );  
    $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'posttype_widget' );  
    $this->WP_Widget( 'posttype_widget', __('Posts by Post Type', 'postType_widget'), $widget_ops, $control_ops );  
}