<?php
/*
*阿树工作室-小工具类文件
*/
//类
class HotPost extends WP_Widget{
	function HotPost(){
		$widget_ops = array('classname'=>'widget_smzdm_hot_post','description'=>'自定义小工具-热门文章');
		$control_ops = array('width'=>300);
		$this->WP_Widget(false,'热门文章',$widget_ops,$control_ops);
	}
	//表单
	function form($instance){
		$instance = wp_parse_args((array)$instance,array(
		'title'=>'热门文章',
		'count'=>5,
		'post_type'=>'',
		));
		
		$output = '<p><lable>标题</lable>';
		$output .= '<input id="'.$this->get_field_id('title') .'" name="'.$this->get_field_name('title').'" type="text" value="'.$instance['title'].'" /></p>';
		$output .= '<p><lable>数量</lable>';
		$output .= '<input id="'.$this->get_field_id('count') .'" name="'.$this->get_field_name('count').'" type="text" value="'.$instance['count'].'" /></p>';
		$output .= '<p><lable>文章类型</lable>';
		$output .= '<select class="postform" id="'.$this->get_field_id('post_type') .'" name="'.$this->get_field_name('post_type').'">';
			if($instance['post_type']=='post'){
				$output .= "<option selected='selected' value='post'>默认文章</option>";
				$output .= "<option value='detail'>爆料(投稿)</option>";
			}else{
				$output .= "<option value='post'>默认文章</option>";
				$output .= "<option selected='selected' value='detail'>爆料(投稿)</option>";
			}
		$output .= '</select>';
		echo $output;
	}
	
	function update($new_instance,$old_instance){
		$instance = $old_instance;
		$instance['title'] = $new_instance['title'];
		$instance['count'] = (int)$new_instance['count'];
		$instance["post_type"] = $new_instance["post_type"];
			
		return $instance;
	}
	
	function widget($args,$instance){
		extract($args);

		echo $before_widget;
		if( $instance['title'] != '' )
		echo $before_title . $instance['title'] . $after_title;
			//echo '<div class="full" style="display:none;"><a class="faxian_href" href="" target="_blank" ><img class="faxian_widget_img" width="250" height="250"/><div class="faxian_widget_title"></div></a></div>';
			$count = (int)$instance['count'];
			if($count<1 || $count=='' ){
				$count = 5;
			}
			$args = array(
				'post_type'=>$instance['post_type'],
				'showposts'=>$count,
				'orderby' => 'comment_count',
			);
			query_posts($args);
			if(have_posts()){
			echo '<div id="slide_wrapper"><div id="slide"><div id="panel"><ul>';
			$count = 0;
			while(have_posts()){
				$count++;
				the_post(); ?>
				<li id="slide_<?php echo $count;?>">
				<div class="slide_pic_wrap"><a class="pic" rel="nofollow" target="_blank" title="<?php the_title(); ?>" href="<?php echo get_permalink($post->ID);?>"> 
				<?php
				if( has_post_thumbnail() ){
					$thumb_id = get_post_thumbnail_id($post->ID);
					$thumb = wp_get_attachment_image_src($thumb_id,array(250, 250));
					$url =  $thumb[0];
				}else{
					$url =get_post_img($post->ID, 250, 250 );
				}
				?>
				<img src="<?php echo $url;?>" height="250" width="250" title="<?php the_title(); ?>" alt="<?php the_title(); ?>" />
				</a>
				</div>
				</li>
			<?php
			}
			echo '</ul></div><div id="slide_bg" style="height: 60px;"></div><div id="slide_text"><a rel="nofollow" title="" target="_blank" href="#"></a></div></div><a id="slide_perv" herf="#" onclick="javascript:pre_img();" style="opacity: 0.3;"></a><a id="slide_next" herf="#" onclick="javascript:next_img();" style="opacity: 0.3;"></a></div>';
			}
		wp_reset_query();
		echo $after_widget;
	}
}
?>