// Custom Widget - Display posts from a specific category // Version: 1.2 // Author: Terry Mun // Author URI: http://www.teddy-o-ted.com/ // Load Custom Theme Widgets function custom_widgets() { register_widget('posts_in_category'); } add_action('widgets_init', 'custom_widgets'); class posts_in_category extends WP_Widget { // Custom Theme Widget: Display posts from a specific category function posts_in_category() { $widget_ops = array( 'classname' => 'posts-in-category', 'description' => __('A widget that displays posts from a specific category', 'posts-in-category') ); $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'posts-in-category-widget' ); $this->WP_Widget( 'posts-in-category-widget', __('List Posts in a Specific Category', 'posts-in-category'), $widget_ops, $control_ops ); } // Displaying widget on screen function widget($args, $instance) { extract($args); $title = apply_filters('widget_title', $instance['title'] ); $category_id = $instance['category_id']; $post_count_limit = $instance['post_count_limit']; $post_orderby = $instance['post_orderby']; $post_offset = $instance['post_offset']; echo $before_widget; if ($title) { echo $before_title . $title . $after_title; } $category_posts = get_posts('category=' . $category_id . '&numberposts=' . $post_count_limit . '&orderby=' . $post_orderby . '&offset= ' . $post_offset . ''); if ($category_posts) { $category_posts_result .= ''; } else { $category_posts_result .= "No posts found in this category"; } echo $category_posts_result; echo $after_widget; } // Update widget settings function update($new_instance, $old_instance) { $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); $instance['category_id'] = strip_tags($new_instance['category_id']); $instance['post_count_limit'] = strip_tags($new_instance['post_count_limit']); $instance['post_offset'] = strip_tags($new_instance['post_offset']); $instance['post_orderby'] = $new_instance['post_orderby']; return $instance; } // Widget setting controls function form($instance) { $defaults = array('title' => __('Category Name', 'posts-in-category'), 'category_id' => __('1', 'posts-in-category'), 'post_count_limit' => __('5', 'posts-in-category'), 'post_orderby' => 'date', 'post_offset' => __('0', 'posts-in-category')); $instance = wp_parse_args((array) $instance, $defaults); ?>