| 1 | <?php |
|---|
| 2 | add_action( 'widgets_init', 'wiles_category_posts_widget' ); |
|---|
| 3 | function wiles_category_posts_widget() { |
|---|
| 4 | register_widget( 'wiles_category_posts' ); |
|---|
| 5 | } |
|---|
| 6 | class wiles_category_posts extends WP_Widget { |
|---|
| 7 | |
|---|
| 8 | function wiles_category_posts() { |
|---|
| 9 | $txt = sprintf(__(' - Category Posts', 'wiles')); |
|---|
| 10 | $widget_ops = array( 'classname' => 'wiles-category-posts' ); |
|---|
| 11 | $control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'wiles-category-posts-widget' ); |
|---|
| 12 | $this->WP_Widget( 'wiles-category-posts-widget',theme_name . $txt, $widget_ops, $control_ops ); |
|---|
| 13 | } |
|---|
| 14 | |
|---|
| 15 | function widget( $args, $instance ) { |
|---|
| 16 | extract( $args ); |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | $title = apply_filters('widget_title', $instance['cat_posts_title'] ); |
|---|
| 20 | $no_of_posts = $instance['no_of_posts']; |
|---|
| 21 | $cats_id = $instance['cats_id']; |
|---|
| 22 | |
|---|
| 23 | echo $before_widget; |
|---|
| 24 | if ( $title){ |
|---|
| 25 | echo $before_title; |
|---|
| 26 | echo $title ; |
|---|
| 27 | echo $after_title;} ?> |
|---|
| 28 | <ul> |
|---|
| 29 | <?php wiles_last_posts_cat($no_of_posts , $cats_id)?> |
|---|
| 30 | </ul> |
|---|
| 31 | <div class="clear"></div> |
|---|
| 32 | <?php |
|---|
| 33 | echo $after_widget; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | function update( $new_instance, $old_instance ) { |
|---|
| 37 | $instance = $old_instance; |
|---|
| 38 | $instance['cat_posts_title'] = strip_tags( $new_instance['cat_posts_title'] ); |
|---|
| 39 | $instance['no_of_posts'] = strip_tags( $new_instance['no_of_posts'] ); |
|---|
| 40 | $instance['cats_id'] = implode(',' , $new_instance['cats_id'] ); |
|---|
| 41 | |
|---|
| 42 | return $instance; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | function form( $instance ) { |
|---|
| 46 | $defaults = array( 'cat_posts_title' =>__( 'Category Posts' , 'wiles'), 'no_of_posts' => '5' , 'cats_id' => '1' ); |
|---|
| 47 | $instance = wp_parse_args( (array) $instance, $defaults ); |
|---|
| 48 | |
|---|
| 49 | $categories_obj = get_categories(); |
|---|
| 50 | $categories = array(); |
|---|
| 51 | |
|---|
| 52 | foreach ($categories_obj as $pn_cat) { |
|---|
| 53 | $categories[$pn_cat->cat_ID] = $pn_cat->cat_name; |
|---|
| 54 | } |
|---|
| 55 | ?> |
|---|
| 56 | <p> |
|---|
| 57 | <label for="<?php echo $this->get_field_id( 'cat_posts_title' ); ?>"><?php _e('Title:', 'wiles'); ?> </label> |
|---|
| 58 | <input id="<?php echo $this->get_field_id( 'cat_posts_title' ); ?>" name="<?php echo $this->get_field_name( 'cat_posts_title' ); ?>" value="<?php echo $instance['cat_posts_title']; ?>" class="widefat" type="text" /> |
|---|
| 59 | </p> |
|---|
| 60 | <p> |
|---|
| 61 | <label for="<?php echo $this->get_field_id( 'no_of_posts' ); ?>"><?php _e('Number of posts to show:', 'wiles'); ?> </label> |
|---|
| 62 | <input id="<?php echo $this->get_field_id( 'no_of_posts' ); ?>" name="<?php echo $this->get_field_name( 'no_of_posts' ); ?>" value="<?php echo $instance['no_of_posts']; ?>" type="text" size="3" /> |
|---|
| 63 | </p> |
|---|
| 64 | <p> |
|---|
| 65 | <?php $cats_id = explode ( ',' , $instance['cats_id'] ) ; ?> |
|---|
| 66 | <label for="<?php echo $this->get_field_id( 'cats_id' ); ?>"><?php _e('Category:', 'wiles'); ?> </label> |
|---|
| 67 | <select multiple="multiple" id="<?php echo $this->get_field_id( 'cats_id' ); ?>[]" name="<?php echo $this->get_field_name( 'cats_id' ); ?>[]"> |
|---|
| 68 | <?php foreach ($categories as $key => $option) { ?> |
|---|
| 69 | <option value="<?php echo $key ?>" <?php if ( in_array( $key , $cats_id ) ) { echo ' selected="selected"' ; } ?>><?php echo $option; ?></option> |
|---|
| 70 | <?php } ?> |
|---|
| 71 | </select> |
|---|
| 72 | </p> |
|---|
| 73 | <?php |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | ?> |
|---|