Category Posts Widget - Version 1.0

Version Description

Download this release

Release Info

Developer ZephyrWest
Plugin Icon 128x128 Category Posts Widget
Version 1.0
Comparing to
See all releases

Version 1.0

Files changed (4) hide show
  1. cat-posts.php +108 -0
  2. readme.txt +30 -0
  3. screenshot-1.png +0 -0
  4. screenshot-2.png +0 -0
cat-posts.php ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: Category Posts Widget
4
+ Plugin URI: http://jameslao.com/
5
+ Description: Adds a widget that can display a specified number of posts from a single category. Can also set how many widgets to show.
6
+ Author: James Lao
7
+ Version: 1.0
8
+ Author URI: http://jameslao.com/
9
+ */
10
+
11
+ // The widget itself.
12
+ function nk_cat_posts_widget($args, $number = 1) {
13
+ extract($args);
14
+ $options = get_option('widget_cat_posts');
15
+ $title = empty($options[$number]['title']) ? 'Category' : $options[$number]['title'];
16
+ $cat_id = empty($options[$number]['cat']) ? 1 : $options[$number]['cat'];
17
+ $num = $options[$number]['num'] > 15 ? 15 : $options[$number]['num'];
18
+
19
+ echo $before_widget;
20
+ echo $before_title . $title . $after_title;
21
+ echo '<ul>';
22
+ nk_cat_posts($cat_id, $num);
23
+ echo '</ul>';
24
+ echo $after_widget;
25
+ }
26
+
27
+ // The control dialog.
28
+ function nk_cat_posts_widget_control($number) {
29
+ $options = $newoptions = get_option('widget_cat_posts');
30
+ if ( $_POST["cat-posts-title-" . $number] ) {
31
+ $newoptions[$number]['title'] = strip_tags(stripslashes($_POST["cat-posts-title-" . $number]));
32
+ $newoptions[$number]['cat'] = $_POST["show-cat-id-" . $number];
33
+ $newoptions[$number]['num'] = is_numeric($_POST["cat-posts-num-" . $number]) && $_POST["cat-posts-num-" . $number]!=0 ? $_POST["cat-posts-num-" . $number] : 5;
34
+ }
35
+ if ( $options != $newoptions ) {
36
+ $options = $newoptions;
37
+ update_option('widget_cat_posts', $options);
38
+ }
39
+
40
+ echo '<p><label for="cat-posts-title-' . $number . '">Title: <input style="width:200px;" id="cat-posts-title-' . $number . '" name="cat-posts-title-' . $number . '" type="text" value="' . $options[$number]['title'] . '" /></label></p>';
41
+ echo '<p><label>Show posts in ';
42
+ wp_dropdown_categories(array('name'=>'show-cat-id-' . $number, 'selected'=>$options[$number]['cat']));
43
+ echo '</label></p>';
44
+ echo '<p><label for="cat-posts-num-' . $number . '">Number of posts to show: <input size="3" id="cat-posts-num-' . $number . '" name="cat-posts-num-' . $number . '" type="text" value="' . $options[$number]['num'] . '" /></label> (max 15)</p>';
45
+ }
46
+
47
+ // Displays the dialog to set how many widgets.
48
+ function nk_cat_posts_widget_page() {
49
+ $options = $newoptions = get_option('widget_cat_posts');
50
+ ?>
51
+ <div class="wrap">
52
+ <form method="post">
53
+ <h2>Category Posts Widgets</h2>
54
+ <p style="line-height: 30px;">How many category posts widgets would you like?
55
+ <select id="cat-posts-number" name="cat-posts-number" value="<?php echo $options['num_of_widgets']; ?>">
56
+ <?php for ( $i = 1; $i < 10; ++$i ) echo "<option value='$i' ".($options['num_of_widgets']==$i ? "selected='selected'" : '').">$i</option>"; ?>
57
+ </select>
58
+ <span class="submit"><input type="submit" name="cat-posts-number-submit" id="cat-posts-number-submit" value="<?php echo attribute_escape(__('Save')); ?>" /></span></p>
59
+ </form>
60
+ </div>
61
+ <?php
62
+ }
63
+
64
+ // Saves how many widgets to be displayed.
65
+ function nk_cat_posts_widget_setup() {
66
+ $options = $newoptions = get_option('widget_cat_posts');
67
+ if ( isset($_POST['cat-posts-number-submit']) ) {
68
+ $number = (int) $_POST['cat-posts-number'];
69
+ if ( $number > 9 ) $number = 9;
70
+ if ( $number < 1 ) $number = 1;
71
+ $newoptions['num_of_widgets'] = $number;
72
+ }
73
+ if ( $options != $newoptions ) {
74
+ $options = $newoptions;
75
+ update_option('widget_cat_posts', $options);
76
+ nk_cat_posts_widget_register($options['num_of_widgets']);
77
+ }
78
+ }
79
+
80
+ // Registers the widget(s).
81
+ function nk_cat_posts_widget_register() {
82
+ $options = get_option('widget_cat_posts');
83
+ $num_of_widgets = $options['num_of_widgets'];
84
+ if ( $num_of_widgets < 1 ) $num_of_widgets = 1;
85
+ if ( $num_of_widgets > 9 ) $num_of_widgets = 9;
86
+ $dims = array('width' => 300, 'height' => 150);
87
+ $class = array('classname' => 'widget_cat_posts');
88
+ for ($i = 1; $i <= 9; $i++) {
89
+ $name = sprintf('Category Posts %d', $i);
90
+ $id = "cat-posts-$i";
91
+ wp_register_sidebar_widget($id, $name, $i <= $num_of_widgets ? 'nk_cat_posts_widget' : '', $class, $i);
92
+ wp_register_widget_control($id, $name, $i <= $num_of_widgets ? 'nk_cat_posts_widget_control' : '', $dims, $i);
93
+ }
94
+ add_action('sidebar_admin_setup', 'nk_cat_posts_widget_setup');
95
+ add_action('sidebar_admin_page', 'nk_cat_posts_widget_page');
96
+ }
97
+
98
+ function nk_cat_posts($cat, $num = 5) {
99
+ $catposts = get_posts('numberposts='.$num.'&category='.$cat);
100
+
101
+ foreach($catposts as $post) {
102
+ echo '<li><a href="'.get_permalink($post).'">'.$post->post_title.'</a></li>';
103
+ }
104
+ }
105
+
106
+ add_action('widgets_init', 'nk_cat_posts_widget_register');
107
+
108
+ ?>
readme.txt ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Plugin Name ===
2
+ Contributors: James Lao
3
+ Donate link: http://jameslao.com/
4
+ Tags: category, posts, widget
5
+ Requires at least: 2.2
6
+ Tested up to: 2.3
7
+ Stable tag: 1.0
8
+
9
+ Adds a widget that shows the most recent posts in a single category. You can specify how many posts to show and from which category as well as how many widgets to show.
10
+
11
+ == Description ==
12
+
13
+ Category Posts Widget is a light widget designed to do one thing and do it well: display the most recent posts from a certain category.
14
+
15
+ **Features:**
16
+ * Specify how many posts to show
17
+ * Set which category the posts should come form
18
+ * Designate how many of the widgets you need
19
+
20
+ == Installation ==
21
+
22
+ 1. Download the plugin.
23
+ 2. Upload it to the plugins folder of your blog.
24
+ 3. Goto the Plugins section of the WordPress admin and activate the plugin.
25
+ 4. Goto the Widget tab of the Presentation section and configure the widget.
26
+
27
+ == Screenshots ==
28
+
29
+ 1. A look at the configuration dialog for a Category Posts widget.
30
+ 2. You can set how many widgets you want.
screenshot-1.png ADDED
Binary file
screenshot-2.png ADDED
Binary file