PluginProbe
Category Posts Block / 3.3
Category Posts Block v3.3
5.0.0 trunk 1.0 1.1 1.2 1.2.1 1.3 1.3.1 1.3.2 1.3.3 2.0 2.1 2.2 2.3 3.1 3.2 3.3 4.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 All 62 releases
category-posts / cat-posts.php

cat-posts.php in Category Posts Block 3.3, at cat-posts.php

263 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Category Posts Widget
4 Plugin URI: http://jameslao.com/2011/03/24/category-posts-widget-3-2/
5 Description: Adds a widget that can display posts from a single category.
6 Author: James Lao
7 Version: 3.3
8 Author URI: http://jameslao.com/
9 */
10
11 // Register thumbnail sizes.
12 if ( function_exists('add_image_size') )
13 {
14 $sizes = get_option('jlao_cat_post_thumb_sizes');
15 if ( $sizes )
16 {
17 foreach ( $sizes as $id=>$size )
18 add_image_size( 'cat_post_thumb_size' . $id, $size[0], $size[1], true );
19 }
20 }
21
22 class CategoryPosts extends WP_Widget {
23
24 function CategoryPosts() {
25 parent::WP_Widget(false, $name='Category Posts');
26 }
27
28 /**
29 * Displays category posts widget on blog.
30 */
31 function widget($args, $instance) {
32 global $post;
33 $post_old = $post; // Save the post object.
34
35 extract( $args );
36
37 $sizes = get_option('jlao_cat_post_thumb_sizes');
38
39 // If not title, use the name of the category.
40 if( !$instance["title"] ) {
41 $category_info = get_category($instance["cat"]);
42 $instance["title"] = $category_info->name;
43 }
44
45 $valid_sort_orders = array('date', 'title', 'comment_count', 'rand');
46 if ( in_array($instance['sort_by'], $valid_sort_orders) ) {
47 $sort_by = $instance['sort_by'];
48 $sort_order = (bool) $instance['asc_sort_order'] ? 'ASC' : 'DESC';
49 } else {
50 // by default, display latest first
51 $sort_by = 'date';
52 $sort_order = 'DESC';
53 }
54
55 // Get array of post info.
56 $cat_posts = new WP_Query(
57 "showposts=" . $instance["num"] .
58 "&cat=" . $instance["cat"] .
59 "&orderby=" . $sort_by .
60 "&order=" . $sort_order
61 );
62
63 // Excerpt length filter
64 $new_excerpt_length = create_function('$length', "return " . $instance["excerpt_length"] . ";");
65 if ( $instance["excerpt_length"] > 0 )
66 add_filter('excerpt_length', $new_excerpt_length);
67
68 echo $before_widget;
69
70 // Widget title
71 echo $before_title;
72 if( $instance["title_link"] )
73 echo '<a href="' . get_category_link($instance["cat"]) . '">' . $instance["title"] . '</a>';
74 else
75 echo $instance["title"];
76 echo $after_title;
77
78 // Post list
79 echo "<ul>\n";
80
81 while ( $cat_posts->have_posts() )
82 {
83 $cat_posts->the_post();
84 ?>
85 <li class="cat-post-item">
86 <a class="post-title" href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
87
88 <?php
89 if (
90 function_exists('the_post_thumbnail') &&
91 current_theme_supports("post-thumbnails") &&
92 $instance["thumb"] &&
93 has_post_thumbnail()
94 ) :
95 ?>
96 <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
97 <?php the_post_thumbnail( 'cat_post_thumb_size'.$this->id ); ?>
98 </a>
99 <?php endif; ?>
100
101 <?php if ( $instance['date'] ) : ?>
102 <p class="post-date"><?php the_time("j M Y"); ?></p>
103 <?php endif; ?>
104
105 <?php if ( $instance['excerpt'] ) : ?>
106 <?php the_excerpt(); ?>
107 <?php endif; ?>
108
109 <?php if ( $instance['comment_num'] ) : ?>
110 <p class="comment-num">(<?php comments_number(); ?>)</p>
111 <?php endif; ?>
112 </li>
113 <?php
114 }
115
116 echo "</ul>\n";
117
118 echo $after_widget;
119
120 remove_filter('excerpt_length', $new_excerpt_length);
121
122 $post = $post_old; // Restore the post object.
123 }
124
125 /**
126 * Form processing... Dead simple.
127 */
128 function update($new_instance, $old_instance) {
129 /**
130 * Save the thumbnail dimensions outside so we can
131 * register the sizes easily. We have to do this
132 * because the sizes must registered beforehand
133 * in order for WP to hard crop images (this in
134 * turn is because WP only hard crops on upload).
135 * The code inside the widget is executed only when
136 * the widget is shown so we register the sizes
137 * outside of the widget class.
138 */
139 if ( function_exists('the_post_thumbnail') )
140 {
141 $sizes = get_option('jlao_cat_post_thumb_sizes');
142 if ( !$sizes ) $sizes = array();
143 $sizes[$this->id] = array($new_instance['thumb_w'], $new_instance['thumb_h']);
144 update_option('jlao_cat_post_thumb_sizes', $sizes);
145 }
146
147 return $new_instance;
148 }
149
150 /**
151 * The configuration form.
152 */
153 function form($instance) {
154 ?>
155 <p>
156 <label for="<?php echo $this->get_field_id("title"); ?>">
157 <?php _e( 'Title' ); ?>:
158 <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"]); ?>" />
159 </label>
160 </p>
161
162 <p>
163 <label>
164 <?php _e( 'Category' ); ?>:
165 <?php wp_dropdown_categories( array( 'name' => $this->get_field_name("cat"), 'selected' => $instance["cat"] ) ); ?>
166 </label>
167 </p>
168
169 <p>
170 <label for="<?php echo $this->get_field_id("num"); ?>">
171 <?php _e('Number of posts to show'); ?>:
172 <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' />
173 </label>
174 </p>
175
176 <p>
177 <label for="<?php echo $this->get_field_id("sort_by"); ?>">
178 <?php _e('Sort by'); ?>:
179 <select id="<?php echo $this->get_field_id("sort_by"); ?>" name="<?php echo $this->get_field_name("sort_by"); ?>">
180 <option value="date"<?php selected( $instance["sort_by"], "date" ); ?>>Date</option>
181 <option value="title"<?php selected( $instance["sort_by"], "title" ); ?>>Title</option>
182 <option value="comment_count"<?php selected( $instance["sort_by"], "comment_count" ); ?>>Number of comments</option>
183 <option value="rand"<?php selected( $instance["sort_by"], "rand" ); ?>>Random</option>
184 </select>
185 </label>
186 </p>
187
188 <p>
189 <label for="<?php echo $this->get_field_id("asc_sort_order"); ?>">
190 <input type="checkbox" class="checkbox"
191 id="<?php echo $this->get_field_id("asc_sort_order"); ?>"
192 name="<?php echo $this->get_field_name("asc_sort_order"); ?>"
193 <?php checked( (bool) $instance["asc_sort_order"], true ); ?> />
194 <?php _e( 'Reverse sort order (ascending)' ); ?>
195 </label>
196 </p>
197
198 <p>
199 <label for="<?php echo $this->get_field_id("title_link"); ?>">
200 <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 ); ?> />
201 <?php _e( 'Make widget title link' ); ?>
202 </label>
203 </p>
204
205 <p>
206 <label for="<?php echo $this->get_field_id("excerpt"); ?>">
207 <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 ); ?> />
208 <?php _e( 'Show post excerpt' ); ?>
209 </label>
210 </p>
211
212 <p>
213 <label for="<?php echo $this->get_field_id("excerpt_length"); ?>">
214 <?php _e( 'Excerpt length (in words):' ); ?>
215 </label>
216 <input style="text-align: center;" type="text" id="<?php echo $this->get_field_id("excerpt_length"); ?>" name="<?php echo $this->get_field_name("excerpt_length"); ?>" value="<?php echo $instance["excerpt_length"]; ?>" size="3" />
217 </p>
218
219 <p>
220 <label for="<?php echo $this->get_field_id("comment_num"); ?>">
221 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("comment_num"); ?>" name="<?php echo $this->get_field_name("comment_num"); ?>"<?php checked( (bool) $instance["comment_num"], true ); ?> />
222 <?php _e( 'Show number of comments' ); ?>
223 </label>
224 </p>
225
226 <p>
227 <label for="<?php echo $this->get_field_id("date"); ?>">
228 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("date"); ?>" name="<?php echo $this->get_field_name("date"); ?>"<?php checked( (bool) $instance["date"], true ); ?> />
229 <?php _e( 'Show post date' ); ?>
230 </label>
231 </p>
232
233 <?php if ( function_exists('the_post_thumbnail') && current_theme_supports("post-thumbnails") ) : ?>
234 <p>
235 <label for="<?php echo $this->get_field_id("thumb"); ?>">
236 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("thumb"); ?>" name="<?php echo $this->get_field_name("thumb"); ?>"<?php checked( (bool) $instance["thumb"], true ); ?> />
237 <?php _e( 'Show post thumbnail' ); ?>
238 </label>
239 </p>
240 <p>
241 <label>
242 <?php _e('Thumbnail dimensions'); ?>:<br />
243 <label for="<?php echo $this->get_field_id("thumb_w"); ?>">
244 W: <input class="widefat" style="width:40%;" type="text" id="<?php echo $this->get_field_id("thumb_w"); ?>" name="<?php echo $this->get_field_name("thumb_w"); ?>" value="<?php echo $instance["thumb_w"]; ?>" />
245 </label>
246
247 <label for="<?php echo $this->get_field_id("thumb_h"); ?>">
248 H: <input class="widefat" style="width:40%;" type="text" id="<?php echo $this->get_field_id("thumb_h"); ?>" name="<?php echo $this->get_field_name("thumb_h"); ?>" value="<?php echo $instance["thumb_h"]; ?>" />
249 </label>
250 </label>
251 </p>
252 <?php endif; ?>
253
254 <?php
255
256 }
257
258 }
259
260 add_action( 'widgets_init', create_function('', 'return register_widget("CategoryPosts");') );
261
262 ?>
263