<?php
add_action( 'widgets_init', 'qaleb_box_4_widget' );
function qaleb_box_4_widget() {
	register_widget( 'qaleb_box_4_widget' );
}

class qaleb_box_4_widget extends WP_Widget {

	public function __construct(){
		$widget_ops = array( 'classname' => 'qaleb_box_4_widget','description' => 'نوشته بلوک بندی'  );
		$control_ops = array( 'id_base' => 'qaleb_box_4_widget' );
		parent::__construct( 'qaleb_box_4_widget',''. __( 'Jam - نوشته طرح چهارم' ), $widget_ops, $control_ops );
	}
	
	function widget( $args, $instance ) {
		extract( $args );		

		$title     = $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
		$category  = $instance['category'];
		$number    = $instance['number'];
?>
<div class="tree clearfix">
<?php
    $my_query = new WP_Query('showposts='.$number.'&cat='.$category);
    while ($my_query->have_posts()):  $my_query->the_post(); ?>
<div class="item">
<div class="thumbnail">
<?php if(has_post_thumbnail()){?>
<a href="<?php the_permalink(); ?>"><img src="<?php the_post_thumbnail_url('loop-2'); ?>"></a>
<?php }else{ ?>
<img src="<?php bloginfo("template_url"); ?>/img/none.png">
<?php } ?>
<?php get_template_part('inc/item/date'); ?>
 </div>
<div class="det">
<?php if(get_post_meta( get_the_ID(), 'meta-text-1', true )!=null){ ?><div class="roti"><?php echo get_post_meta( get_the_ID(), 'meta-text-1', true ); ?></div><?php } ?>
<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
       	<?php 
	}

	function update( $new_instance, $old_instance ) {
		$instance              = $old_instance;
		$instance['category']  = wp_strip_all_tags( $new_instance['category'] );
		$instance['number']    = is_numeric( $new_instance['number'] ) ? intval( $new_instance['number'] ) : 5;
		return $instance;
	}

	function form( $instance ) {
		$defaults  = array( 'category' => '', 'number' => 5, 'show_date' => '' );
		$instance  = wp_parse_args( ( array ) $instance, $defaults );
		$title     = $instance['title'];
		$category  = $instance['category'];
		$number    = $instance['number'];
		?>
		<p>
			<label for="<?php echo $this->get_field_id( 'category' ); ?>">دسته بندی :</label>				
			
			<?php

			wp_dropdown_categories( array(

				'orderby'    => 'title',
				'hide_empty' => false,
				'name'       => $this->get_field_name( 'category' ),
				'id'         => $this->get_field_id( 'category' ),
				'class'      => 'widefat',
				'selected'   => $category

			) );

			?>

		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'number' ); ?>">تعداد : </label>
			<input type="text" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo esc_attr( $number ); ?>" size="3" />
		</p>		
		<?php
	
	}
}
?>