Category Posts Widget - Version 2.0

Version Description

Download this release

Release Info

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

Code changes from version 1.3.3 to 2.0

Files changed (2) hide show
  1. cat-posts.php +92 -154
  2. readme.txt +14 -8
cat-posts.php CHANGED
@@ -1,210 +1,148 @@
1
  <?php
2
  /*
3
  Plugin Name: Category Posts Widget
4
- Plugin URI: http://jameslao.com/2008/04/18/category-posts-widget-13/
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.3.3
8
  Author URI: http://jameslao.com/
9
  */
10
 
11
- // Displays widget on blag
12
- // $widget_args: number
13
- // number: which of the several widgets of this type do we mean
14
- function jl_cat_posts_widget( $args, $widget_args = 1 ) {
15
- extract( $args, EXTR_SKIP );
16
- if ( is_numeric($widget_args) )
17
- $widget_args = array( 'number' => $widget_args );
18
- $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
19
- extract( $widget_args, EXTR_SKIP );
20
-
21
- // Data should be stored as array: array( number => data for that instance of the widget, ... )
22
- $options = get_option('widget_cat_posts');
23
- if ( !isset($options[$number]) )
24
- return;
25
-
26
- $cat_id = empty($options[$number]['cat']) ? 1 : $options[$number]['cat'];
27
- $title_link = (bool) $options[$number]['title_link'];
28
- $excerpt = (bool) $options[$number]['excerpt'];
29
- $num = $options[$number]['num']; // Number of posts to show.
30
 
31
  // If not title, use the name of the category.
32
- if( empty($options[$number]['title']) ) {
33
- $category_info = get_category($cat_id);
34
- $title = $category_info->name;
35
- } else {
36
- $title = $options[$number]['title'];
37
  }
38
 
39
  // Get array of post info.
40
- $cat_posts = get_posts('numberposts='.$num.'&category='.$cat_id);
 
41
 
42
  echo $before_widget;
43
- echo $before_title;
44
-
45
- if( $title_link ) {
46
- echo '<a href="' . get_category_link($cat_id) . '">' . $title . '</a>';
47
- } else {
48
- echo $title;
49
- }
50
 
 
 
 
 
 
51
  echo $after_title;
52
- echo '<ul>';
53
- foreach($cat_posts as $post) {
54
- setup_postdata($post);
55
- echo '<li class="cat-posts-item-' . $post->ID . '"><a href="' . get_permalink($post) . '">' . $post->post_title . '</a>';
56
- if( $excerpt ) {
57
- echo '<br />';
58
- if ($post->post_excerpt!=NULL)
59
- echo $post->post_excerpt;
60
- else
61
- the_excerpt();
62
- }
63
- echo '</li>';
64
- }
65
- echo '</ul>';
66
- echo $after_widget;
67
- }
68
-
69
- // Displays form for a particular instance of the widget. Also updates the data after a POST submit
70
- // $widget_args: number
71
- // number: which of the several widgets of this type do we mean
72
- function jl_cat_posts_control( $widget_args = 1 ) {
73
- global $wp_registered_widgets;
74
- static $updated = false; // Whether or not we have already updated the data after a POST submit
75
-
76
- if ( is_numeric($widget_args) )
77
- $widget_args = array( 'number' => $widget_args );
78
- $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
79
- extract( $widget_args, EXTR_SKIP );
80
-
81
- // Data should be stored as array: array( number => data for that instance of the widget, ... )
82
- $options = get_option('widget_cat_posts');
83
- if ( !is_array($options) )
84
- $options = array();
85
 
86
- // We need to update the data
87
- if ( !$updated && !empty($_POST['sidebar']) ) {
88
- // Tells us what sidebar to put the data in
89
- $sidebar = (string) $_POST['sidebar'];
90
-
91
- $sidebars_widgets = wp_get_sidebars_widgets();
92
- if ( isset($sidebars_widgets[$sidebar]) )
93
- $this_sidebar =& $sidebars_widgets[$sidebar];
94
- else
95
- $this_sidebar = array();
96
 
97
- foreach ( $this_sidebar as $_widget_id ) {
98
- // Remove all widgets of this type from the sidebar. We'll add the new data in a second. This makes sure we don't get any duplicate data
99
- // since widget ids aren't necessarily persistent across multiple updates
100
- if ( 'jl_cat_posts_widget' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
101
- $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
102
- if ( !in_array( "cat-posts-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed. "many-$widget_number" is "{id_base}-{widget_number}
103
- unset($options[$widget_number]);
104
- }
105
- }
106
-
107
- foreach ( (array) $_POST['cat-posts'] as $widget_number => $cat_posts_instance ) {
108
- // compile data from $widget_many_instance
109
- $title = wp_specialchars( $cat_posts_instance['title'] );
110
- $options[$widget_number] = array( 'title' => $title, 'cat' => (int) $cat_posts_instance['cat'], 'num' => (int) $cat_posts_instance['num'], 'title_link' => (bool) $cat_posts_instance['title_link'], 'excerpt' => (bool) $cat_posts_instance['excerpt'] );
111
- }
112
 
113
- update_option('widget_cat_posts', $options);
114
 
115
- $updated = true; // So that we don't go through this more than once
116
- }
 
 
 
 
 
 
117
 
 
118
 
119
- // Here we echo out the form
120
- if ( -1 == $number ) { // We echo out a template for a form which can be converted to a specific form later via JS
121
- $title = '';
122
- $cat = '';
123
- $num = 5;
124
- $link = false;
125
- $excerpt = false;
126
- $number = '%i%';
127
- } else {
128
- $title = attribute_escape($options[$number]['title']);
129
- $cat = (int) $options[$number]['cat'];
130
- $num = (int) $options[$number]['num'];
131
- $link = (bool) $options[$number]['title_link'];
132
- $excerpt = (bool) $options[$number]['excerpt'];
133
- }
134
 
135
- // The form has inputs with names like widget-many[$number][something] so that all data for that instance of
136
- // the widget are stored in one $_POST variable: $_POST['widget-many'][$number]
 
 
 
 
 
 
 
 
 
 
 
 
137
  ?>
138
  <p>
139
- <label for="cat-posts-title-<?php echo $number; ?>">
140
- <?php _e( 'Title:' ); ?>
141
- <input class="widefat" id="cat-posts-title-<?php echo $number; ?>" name="cat-posts[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
142
  </label>
143
  </p>
144
 
145
  <p>
146
  <label>
147
- <?php _e( 'Category:' ); ?><br />
148
- <?php wp_dropdown_categories( array( 'name' => 'cat-posts[' . $number . '][cat]', 'selected' => $cat ) ); ?>
149
  </label>
150
  </p>
151
 
152
  <p>
153
- <label for="cat-posts-number-<?php echo $number; ?>">
154
- <?php _e('Number of posts to show:'); ?>
155
- <input style="width: 25px; text-align: center;" id="cat-posts-number-<?php echo $number; ?>" name="cat-posts[<?php echo $number; ?>][num]" type="text" value="<?php echo $num; ?>" />
156
  </label>
157
  </p>
158
 
159
  <p>
160
- <label for="cat-posts-link-<?php echo $number; ?>">
161
- <input type="checkbox" class="checkbox" id="cat-posts-link-<?php echo $number; ?>" name="cat-posts[<?php echo $number; ?>][title_link]"<?php checked( (bool) $link, true ); ?> />
162
  <?php _e( 'Make widget title link' ); ?>
163
  </label>
164
  </p>
165
 
166
  <p>
167
- <label for="cat-posts-excerpt-<?php echo $number; ?>">
168
- <input type="checkbox" class="checkbox" id="cat-posts-excerpt-<?php echo $number; ?>" name="cat-posts[<?php echo $number; ?>][excerpt]"<?php checked( (bool) $excerpt, true ); ?> />
169
  <?php _e( 'Show post excerpt' ); ?>
170
  </label>
171
  </p>
172
 
173
- <!--<input type="hidden" name="cat-posts[<?php echo $number; ?>][submit]" value="1" />-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  <?php
175
 
176
  }
177
 
178
- // Registers each instance of our widget on startup
179
- function jl_cat_posts_register() {
180
- if ( !$options = get_option('widget_cat_posts') )
181
- $options = array();
182
-
183
- $widget_ops = array('classname' => 'cat_posts', 'description' => __('Widget that shows posts from a specific category.'));
184
- $control_ops = array('id_base' => 'cat-posts');
185
- $name = __('Category Posts');
186
-
187
- $registered = false;
188
- foreach ( array_keys($options) as $o ) {
189
- // Old widgets can have null values for some reason
190
- if ( !isset($options[$o]['cat']) ) // we used 'something' above in our exampple. Replace with with whatever your real data are.
191
- continue;
192
-
193
- // $id should look like {$id_base}-{$o}
194
- $id = "cat-posts-$o"; // Never never never translate an id
195
- $registered = true;
196
- wp_register_sidebar_widget( $id, $name, 'jl_cat_posts_widget', $widget_ops, array( 'number' => $o ) );
197
- wp_register_widget_control( $id, $name, 'jl_cat_posts_control', $control_ops, array( 'number' => $o ) );
198
- }
199
-
200
- // If there are none, we register the widget's existance with a generic template
201
- if ( !$registered ) {
202
- wp_register_sidebar_widget( 'cat-posts-1', $name, 'jl_cat_posts_widget', $widget_ops, array( 'number' => -1 ) );
203
- wp_register_widget_control( 'cat-posts-1', $name, 'jl_cat_posts_control', $control_ops, array( 'number' => -1 ) );
204
- }
205
  }
206
 
207
- // This is important
208
- add_action( 'widgets_init', 'jl_cat_posts_register' );
209
 
210
  ?>
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: 2.0
8
  Author URI: http://jameslao.com/
9
  */
10
 
11
+ class CategoryPosts extends WP_Widget {
12
+
13
+ function CategoryPosts() {
14
+ parent::WP_Widget(false, $name='Category Posts');
15
+ }
16
+
17
+ /**
18
+ * Displays category posts widget on blog.
19
+ */
20
+ function widget($args, $instance) {
21
+ extract( $args );
 
 
 
 
 
 
 
 
22
 
23
  // If not title, use the name of the category.
24
+ if( !$instance["title"] ) {
25
+ $category_info = get_category($instance["cat"]);
26
+ $instance["title"] = $category_info->name;
 
 
27
  }
28
 
29
  // Get array of post info.
30
+ query_posts("showposts=" . $instance["num"] . "&cat=" . $instance["cat"]);
31
+ global $post; // So we can get the post ID
32
 
33
  echo $before_widget;
34
+
35
+ // Widget title
 
 
 
 
 
36
 
37
+ echo $before_title;
38
+ if( (bool) $instance["title_link"] )
39
+ echo '<a href="' . get_category_link($instance["cat"]) . '">' . $instance["title"] . '</a>';
40
+ else
41
+ echo $instance["title"];
42
  echo $after_title;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
+ // Post list
45
+ echo "<ul>\n";
46
+
47
+ while ( have_posts() ) : the_post();
48
+ ?>
49
+ <li class='cat-post-item'>
 
 
 
 
50
 
51
+ <?php if ( (bool) $instance["thumbnail"] && function_exists('p75HasThumbnail') && p75HasThumbnail($post->ID) ) { ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
+ <a href="<?php the_permalink(); ?>" title="<?php echo the_title_attribute(); ?>"><img class='alignleft' src='<?php echo p75GetThumbnail($post->ID, $instance["thumbnail_width"], $instance["thumbnail_height"]); ?>' alt='<?php echo the_title_attribute(); ?>' /></a>
54
 
55
+ <?php } // end thumbnail function check ?>
56
+
57
+ <a class="post-title" href="<?php echo the_permalink(); ?>" title="<?php echo the_title_attribute(); ?>"><?php echo the_title(); ?></a>
58
+
59
+ <?php if ( $instance['excerpt'] ) the_excerpt(); ?>
60
+ </li>
61
+ <?php
62
+ endwhile;
63
 
64
+ echo "</ul>\n";
65
 
66
+ echo $after_widget;
67
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
+ /**
70
+ * Form processing... Dead simple.
71
+ */
72
+ function update($new_instance, $old_instance) {
73
+ $new_instance["cat"] = absint( $new_instance["cat"] );
74
+ $new_instance["exclude"] = (bool) $new_instance["exclude"];
75
+
76
+ return $new_instance;
77
+ }
78
+
79
+ /**
80
+ * The configuration form.
81
+ */
82
+ function form($instance) {
83
  ?>
84
  <p>
85
+ <label for="<?php echo $this->get_field_id("title"); ?>">
86
+ <?php _e( 'Title' ); ?>:
87
+ <input class="widefat" id="<?php echo $this->get_field_id("title"); ?>" name="<?php echo $this->get_field_name("title"); ?>" type="text" value="<?php echo esc_attr($instance["title"]); ?>" />
88
  </label>
89
  </p>
90
 
91
  <p>
92
  <label>
93
+ <?php _e( 'Category' ); ?>:
94
+ <?php wp_dropdown_categories( array( 'name' => $this->get_field_name("cat"), 'selected' => $instance["cat"] ) ); ?>
95
  </label>
96
  </p>
97
 
98
  <p>
99
+ <label for="<?php echo $this->get_field_id("num"); ?>">
100
+ <?php _e('Number of posts to show'); ?>:
101
+ <input style="text-align: center;" id="<?php echo $this->get_field_id("num"); ?>" name="<?php echo $this->get_field_name("num"); ?>" type="text" value="<?php echo absint($instance["num"]); ?>" size='3' />
102
  </label>
103
  </p>
104
 
105
  <p>
106
+ <label for="<?php echo $this->get_field_id("title_link"); ?>">
107
+ <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("title_link"); ?>" name="<?php echo $this->get_field_name("title_link"); ?>"<?php checked( (bool) $instance["title_link"], true ); ?> />
108
  <?php _e( 'Make widget title link' ); ?>
109
  </label>
110
  </p>
111
 
112
  <p>
113
+ <label for="<?php echo $this->get_field_id("excerpt"); ?>">
114
+ <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("excerpt"); ?>" name="<?php echo $this->get_field_name("excerpt"); ?>"<?php checked( (bool) $instance["excerpt"], true ); ?> />
115
  <?php _e( 'Show post excerpt' ); ?>
116
  </label>
117
  </p>
118
 
119
+ <?php if ( function_exists("p75GetThumbnail") ) : ?>
120
+ <p>
121
+ <label for="<?php echo $this->get_field_id("thumbnail"); ?>">
122
+ <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("thumbnail"); ?>" name="<?php echo $this->get_field_name("thumbnail"); ?>"<?php checked( (bool) $instance["thumbnail"], true ); ?> />
123
+ <?php _e( 'Show post thumbnail' ); ?>
124
+ </label>
125
+ </p>
126
+ <p>
127
+ <label>
128
+ <?php _e('Thumbnail dimensions'); ?>:<br />
129
+ <label for="<?php echo $this->get_field_id("thumbnail_width"); ?>">
130
+ W: <input class="widefat" style="width:40%;" type="text" id="<?php echo $this->get_field_id("thumbnail_width"); ?>" name="<?php echo $this->get_field_name("thumbnail_width"); ?>" value="<?php echo $instance["thumbnail_width"]; ?>" />
131
+ </label>
132
+
133
+ <label for="<?php echo $this->get_field_id("thumbnail_height"); ?>">
134
+ H: <input class="widefat" style="width:40%;" type="text" id="<?php echo $this->get_field_id("thumbnail_height"); ?>" name="<?php echo $this->get_field_name("thumbnail_height"); ?>" value="<?php echo $instance["thumbnail_height"]; ?>" />
135
+ </label>
136
+ </label>
137
+ </p>
138
+ <?php endif; ?>
139
+
140
  <?php
141
 
142
  }
143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  }
145
 
146
+ add_action( 'widgets_init', create_function('', 'return register_widget("CategoryPosts");') );
 
147
 
148
  ?>
readme.txt CHANGED
@@ -2,24 +2,23 @@
2
  Contributors: James Lao
3
  Donate link: http://jameslao.com/
4
  Tags: category, posts, widget
5
- Requires at least: 2.5
6
- Tested up to: 2.7-beta3
7
- Stable tag: 1.3.3
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
- **NOTICE:** You may have to reconfigure Category Posts if you are upgrading from a previous version to 1.3.
14
-
15
  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.
16
 
17
  Features:
18
-
 
19
  * Specify how many posts to show
20
  * Set which category the posts should come form
21
  * Designate how many of the widgets you need
22
- * **New Feature** Specify whether to make to the widget title a link to the category page
23
  * Optionally show the post excerpt
24
 
25
  == Installation ==
@@ -28,3 +27,10 @@ Features:
28
  2. Upload it to the plugins folder of your blog.
29
  3. Goto the Plugins section of the WordPress admin and activate the plugin.
30
  4. Goto the Widget tab of the Presentation section and configure the widget.
 
 
 
 
 
 
 
2
  Contributors: James Lao
3
  Donate link: http://jameslao.com/
4
  Tags: category, posts, widget
5
+ Requires at least: 2.8
6
+ Tested up to: 2.8.2
7
+ Stable tag: 2.0
8
 
9
+ Adds a widget that shows the most recent posts in a single category.
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
+
17
+ * Support for displaying thumbnail images via [Simple Post Thumbnails plugin](http://wordpress.org/extend/plugins/simple-post-thumbnails/).
18
  * Specify how many posts to show
19
  * Set which category the posts should come form
20
  * Designate how many of the widgets you need
21
+ * Specify whether to make to the widget title a link to the category page
22
  * Optionally show the post excerpt
23
 
24
  == Installation ==
27
  2. Upload it to the plugins folder of your blog.
28
  3. Goto the Plugins section of the WordPress admin and activate the plugin.
29
  4. Goto the Widget tab of the Presentation section and configure the widget.
30
+
31
+ == Changelog ==
32
+
33
+ 2.0
34
+
35
+ * Updated to use the WP 2.8 widget API.
36
+ * Added support for [Simple Post Thumbnails plugin](http://wordpress.org/extend/plugins/simple-post-thumbnails/).