wordpress热门文章

// 热门文章
class hot_post extends WP_Widget {
     function hot_post() {
         $widget_ops = array('description' => '主题自带的热门文章小工具,使用前必须安装 
	 wp-postviews 插件,并且有统计数据');
         $this->WP_Widget('hot_post', '主题  热门文章', $widget_ops);
     }
     function widget($args, $instance) {
		extract($args);
		$title = apply_filters( 'widget_title', $instance['title'] );
		echo $before_widget;
		if ( ! empty( $title ) )
		echo $before_title . $title . $after_title;
		$number = strip_tags($instance['number']) ? absint( $instance['number'] 
		) : 5;
		$days = strip_tags($instance['days']) ? absint( $instance['days'] ) : 
		90;
?>

<div id="hot_post_widget">
	<ul>
	    <?php if (function_exists('get_most_viewed')): ?>
	    <?php get_timespan_most_viewed('post',$number,$days, true, true); ?>
	    <?php endif; ?>
		<?php wp_reset_query(); ?>
	</ul>
</div>

<?php
	echo $after_widget;
	}
	function update( $new_instance, $old_instance ) {
		if (!isset($new_instance['submit'])) {
			return false;
		}
			$instance = $old_instance;
			$instance = array();
			$instance['title'] = strip_tags( $new_instance['title'] );
			$instance['number'] = strip_tags($new_instance['number']);
			$instance['days'] = strip_tags($new_instance['days']);
			return $instance;
		}
	function form($instance) {
		if ( isset( $instance[ 'title' ] ) ) {
			$title = $instance[ 'title' ];
		}
		else {
			$title = '热门文章';
		}
		global $wpdb;
		$instance = wp_parse_args((array) $instance, array('number' => '5'));
		$instance = wp_parse_args((array) $instance, array('days' => '90'));
		$number = strip_tags($instance['number']);
		$days = strip_tags($instance['days']);
 ?>
	<p><label for="<?php echo $this->get_field_id( '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'); ?>">显示数量:</label>
	<input 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><label for="<?php echo $this->get_field_id('days'); ?>">时间限定(天
	):</label>
	<input id="<?php echo $this->get_field_id( 'days' ); ?>" name="<?php echo 
	$this->get_field_name( 'days' ); ?>" type="text" value="<?php echo $days; ?>" 
	size="3" /></p>
	<input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" 
	name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
<?php }
}
add_action( 'widgets_init', create_function( '', 'register_widget( "hot_post" );' ) );