PluginProbe
Category Posts Block / 3.1
Category Posts Block v3.1
5.1.0 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 All 63 releases
category-posts / cat-posts.php

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

226 lines 7.5 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/2009/12/30/category-posts-widget-3-0/
5 Description: Adds a widget that can display posts from a single category.
6 Author: James Lao
7 Version: 3.1
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 // Get array of post info.
46 $cat_posts = new WP_Query("showposts=" . $instance["num"] . "&cat=" . $instance["cat"]);
47
48 // Excerpt length filter
49 $new_excerpt_length = create_function('$length', "return " . $instance["excerpt_length"] . ";");
50 if ( $instance["excerpt_length"] > 0 )
51 add_filter('excerpt_length', $new_excerpt_length);
52
53 echo $before_widget;
54
55 // Widget title
56 echo $before_title;
57 if( $instance["title_link"] )
58 echo '<a href="' . get_category_link($instance["cat"]) . '">' . $instance["title"] . '</a>';
59 else
60 echo $instance["title"];
61 echo $after_title;
62
63 // Post list
64 echo "<ul>\n";
65
66 while ( $cat_posts->have_posts() )
67 {
68 $cat_posts->the_post();
69 ?>
70 <li class="cat-post-item">
71 <a class="post-title" href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
72
73 <?php
74 if (
75 function_exists('the_post_thumbnail') &&
76 current_theme_supports("post-thumbnails") &&
77 $instance["thumb"] &&
78 has_post_thumbnail()
79 ) :
80 ?>
81 <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
82 <?php the_post_thumbnail( 'cat_post_thumb_size'.$this->id ); ?>
83 </a>
84 <?php endif; ?>
85
86 <?php if ( $instance['date'] ) : ?>
87 <p class="post-date"><?php the_time("j M Y"); ?></p>
88 <?php endif; ?>
89
90 <?php if ( $instance['excerpt'] ) : ?>
91 <?php the_excerpt(); ?>
92 <?php endif; ?>
93
94 <?php if ( $instance['comment_num'] ) : ?>
95 <p class="comment-num">(<?php comments_number(); ?>)</p>
96 <?php endif; ?>
97 </li>
98 <?php
99 }
100
101 echo "</ul>\n";
102
103 echo $after_widget;
104
105 remove_filter('excerpt_length', $new_excerpt_length);
106
107 $post = $post_old; // Restore the post object.
108 }
109
110 /**
111 * Form processing... Dead simple.
112 */
113 function update($new_instance, $old_instance) {
114 /**
115 * Save the thumbnail dimensions outside so we can
116 * register the sizes easily. We have to do this
117 * because the sizes must registered beforehand
118 * in order for WP to hard crop images (this in
119 * turn is because WP only hard crops on upload).
120 * The code inside the widget is executed only when
121 * the widget is shown so we register the sizes
122 * outside of the widget class.
123 */
124 if ( function_exists('the_post_thumbnail') )
125 {
126 $sizes = get_option('jlao_cat_post_thumb_sizes');
127 if ( !$sizes ) $sizes = array();
128 $sizes[$this->id] = array($new_instance['thumb_w'], $new_instance['thumb_h']);
129 update_option('jlao_cat_post_thumb_sizes', $sizes);
130 }
131
132 return $new_instance;
133 }
134
135 /**
136 * The configuration form.
137 */
138 function form($instance) {
139 ?>
140 <p>
141 <label for="<?php echo $this->get_field_id("title"); ?>">
142 <?php _e( 'Title' ); ?>:
143 <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"]); ?>" />
144 </label>
145 </p>
146
147 <p>
148 <label>
149 <?php _e( 'Category' ); ?>:
150 <?php wp_dropdown_categories( array( 'name' => $this->get_field_name("cat"), 'selected' => $instance["cat"] ) ); ?>
151 </label>
152 </p>
153
154 <p>
155 <label for="<?php echo $this->get_field_id("num"); ?>">
156 <?php _e('Number of posts to show'); ?>:
157 <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' />
158 </label>
159 </p>
160
161 <p>
162 <label for="<?php echo $this->get_field_id("title_link"); ?>">
163 <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 ); ?> />
164 <?php _e( 'Make widget title link' ); ?>
165 </label>
166 </p>
167
168 <p>
169 <label for="<?php echo $this->get_field_id("excerpt"); ?>">
170 <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 ); ?> />
171 <?php _e( 'Show post excerpt' ); ?>
172 </label>
173 </p>
174
175 <p>
176 <label for="<?php echo $this->get_field_id("excerpt_length"); ?>">
177 <?php _e( 'Excerpt length (in words):' ); ?>
178 </label>
179 <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" />
180 </p>
181
182 <p>
183 <label for="<?php echo $this->get_field_id("comment_num"); ?>">
184 <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 ); ?> />
185 <?php _e( 'Show number of comments' ); ?>
186 </label>
187 </p>
188
189 <p>
190 <label for="<?php echo $this->get_field_id("date"); ?>">
191 <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 ); ?> />
192 <?php _e( 'Show post date' ); ?>
193 </label>
194 </p>
195
196 <?php if ( function_exists('the_post_thumbnail') && current_theme_supports("post-thumbnails") ) : ?>
197 <p>
198 <label for="<?php echo $this->get_field_id("thumb"); ?>">
199 <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 ); ?> />
200 <?php _e( 'Show post thumbnail' ); ?>
201 </label>
202 </p>
203 <p>
204 <label>
205 <?php _e('Thumbnail dimensions'); ?>:<br />
206 <label for="<?php echo $this->get_field_id("thumb_w"); ?>">
207 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"]; ?>" />
208 </label>
209
210 <label for="<?php echo $this->get_field_id("thumb_h"); ?>">
211 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"]; ?>" />
212 </label>
213 </label>
214 </p>
215 <?php endif; ?>
216
217 <?php
218
219 }
220
221 }
222
223 add_action( 'widgets_init', create_function('', 'return register_widget("CategoryPosts");') );
224
225 ?>
226