classwidget_tagsextendsWP_Widget{

    //添加小工具

    function__construct(){

        $this->WP_Widget('random_posts',__('隨機文章','Bing'),array('description'=>__('顯示幾篇隨機文章','Bing')));

    }

    //小工具內容

    functionwidget($args,$instance){

        //導入當前側邊欄設置

        extract($args,EXTR_SKIP);

        //添加小工具標題過濾器

        $title=apply_filters('widget_name',$instance['title']);

        //輸出小工具前代碼

        echo$before_widget;

            //輸出小工具標題

            echo$before_title.$title.$after_title;

            //隨機文章

            query_posts('orderby=rand&showposts='.$instance['number']);

            if(have_posts()):

                echo'<ul>';

                    while(have_posts()):

                        the_post();

                        printf('<li><a href="%s" title="%s">%s</a></li>',get_permalink(),get_the_title(),get_the_title());

                    endwhile;

                echo'</ul>';

            endif;

        //輸出小工具後代碼

        echo$after_widget;

    }

    //更新選項

    functionupdate($instance,$old_instance){

        $instance['title']=strip_tags($instance['title']);

        $instance['number']=(int)strip_tags($instance['number']);

        return$instance;

    }

    //選項表單

    functionform($instance){

        //添加默認設置

        $instance=wp_parse_args($instance,array(

            'title'=>__('隨機文章','Bing'),

            'number'=>10

        ));

        //設置表單

?>

        <p>

            <label for="<?phpecho$this->get_field_id('title');?>"><?php_e('標題','Bing');?></label>

            <input class="widefat"id="<?phpecho$this->get_field_id('title');?>"name="<?phpecho$this->get_field_name('title');?>"type="text"value="<?phpecho$instance['title'];?>"/>

        </p>

        <p>

            <label for="<?phpecho$this->get_field_id('number');?>"><?php_e('文章數量','Bing');?></label>

            <input class="widefat"id="<?phpecho$this->get_field_id('number');?>"name="<?phpecho$this->get_field_name('number');?>"type="number"value="<?phpecho$instance['number'];?>"/>

        </p>

<?php

    }

}

functionBing_add_widget_tags(){

    register_widget('widget_tags');

}

add_action('widgets_init','Bing_add_widget_tags');