I have build, some time before a widget,
Also you can set the number of posts, the category and the order!
<?php
/****** Widget ******/
class Artdev_Category_Posts extends WP_Widget {
function Artdev_Category_Posts() {
$widget_ops = array('description' => 'Add Selected Category Posts to the Sidebar' );
parent::WP_Widget(false, __('→ Category List', 'artdev'),$widget_ops);
}
function widget($args, $instance) {
extract( $args );
$title = esc_attr($instance['title']);
$cat_name = esc_attr($instance['cat_name']);
$posts = esc_attr($instance['posts']);
$order = esc_attr($instance['order']);
echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<?php // The Loop
$wq = new WP_Query();
$wq->query( array( 'post_type' => 'events', 'category_name' => $cat_name, 'posts_per_page' => $posts, 'orderby' => $order ));
if( $wq->have_posts() ) :
?>
<ul class="widgetlist">
<?php while($wq->have_posts()) : $wq->the_post(); ?>
<li>
<h3><a href="https://wordpress.stackexchange.com/questions/36446/<?php the_permalink(); ?>" rel="bookmark"><?php echo get_the_title(); ?></a></h3>
</li>
<?php endwhile; ?>
</ul>
<?php endif; wp_reset_query(); ?>
<?//php } ?>
<?php echo $after_widget;
}
function update($new_instance, $old_instance) {
return $new_instance;
}
function form($instance) {
$title = esc_attr($instance['title']);
$cat_name = esc_attr($instance['cat_name']);
$posts = esc_attr($instance['posts']);
$order = esc_attr($instance['order']);
?>
<p><!-- Widget Title -->
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:','artdev'); ?></label>
<input type="text" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" />
</p>
<p><!-- Category Name -->
<label for="<?php echo $this->get_field_id('cat_name'); ?>"><?php _e('Category Name:','artdev'); ?></label>
<input type="text" name="<?php echo $this->get_field_name('cat_name'); ?>" value="<?php echo $cat_name; ?>" id="<?php echo $this->get_field_id('cat_name'); ?>" class="widefat" />
</p>
<p><!-- Number Of Posts -->
<label for="<?php echo $this->get_field_id('posts'); ?>"><?php _e('Number of posts to show:','artdev'); ?></label>
<input type="text" name="<?php echo $this->get_field_name('posts'); ?>" id="<?php echo $this->get_field_id('posts'); ?>" value="<?php echo $posts; ?>" size="2" />
</p>
<p><!-- Order Posts -->
<label for="<?php echo $this->get_field_id('order'); ?>"><?php _e('Order of posts:','artdev'); ?></label>
<select name="<?php echo $this->get_field_name('order'); ?>">
<option value="title" <?php if ($order == 'title') { echo 'selected="selected"'; } ?>>Order Posts by Title</option>
<option value="ID" <?php if ($order == 'ID') { echo 'selected="selected"'; } ?>>Order Posts by ID</option>
<option value="date" <?php if ($order == 'date') { echo 'selected="selected"'; } ?>>Order Posts by Date</option>
<option value="rand" <?php if ($order == 'rand') { echo 'selected="selected"'; } ?>>Randomize Posts</option>
</select>
</p>
<?php
}
}
register_widget('Artdev_Category_Posts');
?>