Version Description
Download this release
Release Info
Developer | ZephyrWest |
Plugin | Category Posts Widget |
Version | 1.3 |
Comparing to | |
See all releases |
Code changes from version 1.2.1 to 1.3
- cat-posts.php +161 -83
- readme.txt +8 -10
- screenshot-1.png +0 -0
- screenshot-2.png +0 -0
cat-posts.php
CHANGED
@@ -4,126 +4,204 @@ 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.
|
8 |
Author URI: http://jameslao.com/
|
9 |
*/
|
10 |
|
11 |
-
//
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
$options = get_option('widget_cat_posts');
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
// If not title, use the name of the category.
|
19 |
if( empty($options[$number]['title']) ) {
|
20 |
-
$
|
21 |
-
$title = $
|
22 |
} else {
|
23 |
$title = $options[$number]['title'];
|
24 |
}
|
25 |
|
26 |
-
|
|
|
27 |
|
28 |
echo $before_widget;
|
29 |
echo $before_title;
|
30 |
-
|
31 |
-
if( $
|
32 |
-
echo '<a href="' . get_category_link($
|
33 |
} else {
|
34 |
echo $title;
|
35 |
}
|
36 |
-
|
37 |
echo $after_title;
|
38 |
echo '<ul>';
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
echo '</ul>';
|
41 |
echo $after_widget;
|
42 |
}
|
43 |
|
44 |
-
//
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
update_option('widget_cat_posts', $options);
|
|
|
|
|
56 |
}
|
57 |
|
58 |
-
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>';
|
59 |
-
echo '<p><label>Show posts in ';
|
60 |
-
wp_dropdown_categories(array('name'=>'show-cat-id-' . $number, 'selected'=>$options[$number]['cat']));
|
61 |
-
echo '</label></p>';
|
62 |
-
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>';
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
//
|
69 |
-
|
70 |
-
$options = $newoptions = get_option('widget_cat_posts');
|
71 |
?>
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
<?php
|
83 |
-
}
|
84 |
|
85 |
-
// Saves how many widgets to be displayed.
|
86 |
-
function nk_cat_posts_widget_setup() {
|
87 |
-
$options = $newoptions = get_option('widget_cat_posts');
|
88 |
-
if ( isset($_POST['cat-posts-number-submit']) ) {
|
89 |
-
$number = (int) $_POST['cat-posts-number'];
|
90 |
-
if ( $number > 20 ) $number = 20;
|
91 |
-
if ( $number < 1 ) $number = 1;
|
92 |
-
$newoptions['num_of_widgets'] = $number;
|
93 |
-
}
|
94 |
-
if ( $options != $newoptions ) {
|
95 |
-
$options = $newoptions;
|
96 |
-
update_option('widget_cat_posts', $options);
|
97 |
-
nk_cat_posts_widget_register($options['num_of_widgets']);
|
98 |
-
}
|
99 |
}
|
100 |
|
101 |
-
// Registers
|
102 |
-
function
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
$
|
108 |
-
$
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
}
|
115 |
-
add_action('sidebar_admin_setup', 'nk_cat_posts_widget_setup');
|
116 |
-
add_action('sidebar_admin_page', 'nk_cat_posts_widget_page');
|
117 |
-
}
|
118 |
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
echo '<li><a href="'.get_permalink($post).'">'.$post->post_title.'</a></li>';
|
124 |
}
|
125 |
}
|
126 |
|
127 |
-
|
|
|
128 |
|
129 |
?>
|
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.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 |
+
the_excerpt();
|
59 |
+
}
|
60 |
+
echo '</li>';
|
61 |
+
}
|
62 |
echo '</ul>';
|
63 |
echo $after_widget;
|
64 |
}
|
65 |
|
66 |
+
// Displays form for a particular instance of the widget. Also updates the data after a POST submit
|
67 |
+
// $widget_args: number
|
68 |
+
// number: which of the several widgets of this type do we mean
|
69 |
+
function jl_cat_posts_control( $widget_args = 1 ) {
|
70 |
+
global $wp_registered_widgets;
|
71 |
+
static $updated = false; // Whether or not we have already updated the data after a POST submit
|
72 |
+
|
73 |
+
if ( is_numeric($widget_args) )
|
74 |
+
$widget_args = array( 'number' => $widget_args );
|
75 |
+
$widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
|
76 |
+
extract( $widget_args, EXTR_SKIP );
|
77 |
+
|
78 |
+
// Data should be stored as array: array( number => data for that instance of the widget, ... )
|
79 |
+
$options = get_option('widget_cat_posts');
|
80 |
+
if ( !is_array($options) )
|
81 |
+
$options = array();
|
82 |
+
|
83 |
+
// We need to update the data
|
84 |
+
if ( !$updated && !empty($_POST['sidebar']) ) {
|
85 |
+
// Tells us what sidebar to put the data in
|
86 |
+
$sidebar = (string) $_POST['sidebar'];
|
87 |
+
|
88 |
+
$sidebars_widgets = wp_get_sidebars_widgets();
|
89 |
+
if ( isset($sidebars_widgets[$sidebar]) )
|
90 |
+
$this_sidebar =& $sidebars_widgets[$sidebar];
|
91 |
+
else
|
92 |
+
$this_sidebar = array();
|
93 |
+
|
94 |
+
foreach ( $this_sidebar as $_widget_id ) {
|
95 |
+
// 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
|
96 |
+
// since widget ids aren't necessarily persistent across multiple updates
|
97 |
+
if ( 'jl_cat_posts_widget' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
|
98 |
+
$widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
|
99 |
+
if ( !in_array( "cat-posts-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed. "many-$widget_number" is "{id_base}-{widget_number}
|
100 |
+
unset($options[$widget_number]);
|
101 |
+
}
|
102 |
+
}
|
103 |
+
|
104 |
+
foreach ( (array) $_POST['cat-posts'] as $widget_number => $cat_posts_instance ) {
|
105 |
+
// compile data from $widget_many_instance
|
106 |
+
$title = wp_specialchars( $cat_posts_instance['title'] );
|
107 |
+
$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'] );
|
108 |
+
}
|
109 |
+
|
110 |
update_option('widget_cat_posts', $options);
|
111 |
+
|
112 |
+
$updated = true; // So that we don't go through this more than once
|
113 |
}
|
114 |
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
+
// Here we echo out the form
|
117 |
+
if ( -1 == $number ) { // We echo out a template for a form which can be converted to a specific form later via JS
|
118 |
+
$title = '';
|
119 |
+
$cat = '';
|
120 |
+
$num = 5;
|
121 |
+
$link = false;
|
122 |
+
$excerpt = false;
|
123 |
+
$number = '%i%';
|
124 |
+
} else {
|
125 |
+
$title = attribute_escape($options[$number]['title']);
|
126 |
+
$cat = (int) $options[$number]['cat'];
|
127 |
+
$num = (int) $options[$number]['num'];
|
128 |
+
$link = (bool) $options[$number]['title_link'];
|
129 |
+
$excerpt = (bool) $options[$number]['excerpt'];
|
130 |
+
}
|
131 |
|
132 |
+
// The form has inputs with names like widget-many[$number][something] so that all data for that instance of
|
133 |
+
// the widget are stored in one $_POST variable: $_POST['widget-many'][$number]
|
|
|
134 |
?>
|
135 |
+
<p>
|
136 |
+
<label for="cat-posts-title-<?php echo $number; ?>">
|
137 |
+
<?php _e( 'Title:' ); ?>
|
138 |
+
<input class="widefat" id="cat-posts-title-<?php echo $number; ?>" name="cat-posts[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
|
139 |
+
</label>
|
140 |
+
</p>
|
141 |
+
|
142 |
+
<p>
|
143 |
+
<label>
|
144 |
+
<?php _e( 'Category:' ); ?><br />
|
145 |
+
<?php wp_dropdown_categories( array( 'name' => 'cat-posts[' . $number . '][cat]', 'selected' => $cat ) ); ?>
|
146 |
+
</label>
|
147 |
+
</p>
|
148 |
+
|
149 |
+
<p>
|
150 |
+
<label for="cat-posts-number-<?php echo $number; ?>">
|
151 |
+
<?php _e('Number of posts to show:'); ?>
|
152 |
+
<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; ?>" />
|
153 |
+
</label>
|
154 |
+
</p>
|
155 |
+
|
156 |
+
<p>
|
157 |
+
<label for="cat-posts-link-<?php echo $number; ?>">
|
158 |
+
<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 ); ?> />
|
159 |
+
<?php _e( 'Make widget title link' ); ?>
|
160 |
+
</label>
|
161 |
+
</p>
|
162 |
+
|
163 |
+
<p>
|
164 |
+
<label for="cat-posts-excerpt-<?php echo $number; ?>">
|
165 |
+
<input type="checkbox" class="checkbox" id="cat-posts-excerpt-<?php echo $number; ?>" name="cat-posts[<?php echo $number; ?>][excerpt]"<?php checked( (bool) $excerpt, true ); ?> />
|
166 |
+
<?php _e( 'Show post excerpt' ); ?>
|
167 |
+
</label>
|
168 |
+
</p>
|
169 |
+
|
170 |
+
<input type="hidden" id="cat-posts-<?php echo $number; ?>" name="cat-posts[<?php echo $number; ?>][submit]" value="1" />
|
171 |
<?php
|
|
|
172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
}
|
174 |
|
175 |
+
// Registers each instance of our widget on startup
|
176 |
+
function jl_cat_posts_register() {
|
177 |
+
if ( !$options = get_option('widget_cat_posts') )
|
178 |
+
$options = array();
|
179 |
+
|
180 |
+
$widget_ops = array('classname' => 'cat_posts', 'description' => __('Widget that shows posts from a specific category.'));
|
181 |
+
$control_ops = array('id_base' => 'cat-posts');
|
182 |
+
$name = __('Category Posts');
|
183 |
+
|
184 |
+
$registered = false;
|
185 |
+
foreach ( array_keys($options) as $o ) {
|
186 |
+
// Old widgets can have null values for some reason
|
187 |
+
if ( !isset($options[$o]['cat']) ) // we used 'something' above in our exampple. Replace with with whatever your real data are.
|
188 |
+
continue;
|
189 |
+
|
190 |
+
// $id should look like {$id_base}-{$o}
|
191 |
+
$id = "cat-posts-$o"; // Never never never translate an id
|
192 |
+
$registered = true;
|
193 |
+
wp_register_sidebar_widget( $id, $name, 'jl_cat_posts_widget', $widget_ops, array( 'number' => $o ) );
|
194 |
+
wp_register_widget_control( $id, $name, 'jl_cat_posts_control', $control_ops, array( 'number' => $o ) );
|
195 |
}
|
|
|
|
|
|
|
196 |
|
197 |
+
// If there are none, we register the widget's existance with a generic template
|
198 |
+
if ( !$registered ) {
|
199 |
+
wp_register_sidebar_widget( 'cat-posts-1', $name, 'jl_cat_posts_widget', $widget_ops, array( 'number' => -1 ) );
|
200 |
+
wp_register_widget_control( 'cat-posts-1', $name, 'jl_cat_posts_control', $control_ops, array( 'number' => -1 ) );
|
|
|
201 |
}
|
202 |
}
|
203 |
|
204 |
+
// This is important
|
205 |
+
add_action( 'widgets_init', 'jl_cat_posts_register' );
|
206 |
|
207 |
?>
|
readme.txt
CHANGED
@@ -2,14 +2,16 @@
|
|
2 |
Contributors: James Lao
|
3 |
Donate link: http://jameslao.com/
|
4 |
Tags: category, posts, widget
|
5 |
-
Requires at least: 2.
|
6 |
-
Tested up to: 2.
|
7 |
-
Stable tag: 1.
|
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:
|
@@ -17,16 +19,12 @@ Features:
|
|
17 |
* Specify how many posts to show
|
18 |
* Set which category the posts should come form
|
19 |
* Designate how many of the widgets you need
|
20 |
-
* Specify whether to make to the widget title a link to the category page
|
|
|
21 |
|
22 |
== Installation ==
|
23 |
|
24 |
1. Download the plugin.
|
25 |
2. Upload it to the plugins folder of your blog.
|
26 |
3. Goto the Plugins section of the WordPress admin and activate the plugin.
|
27 |
-
4. Goto the Widget tab of the Presentation section and configure the widget.
|
28 |
-
|
29 |
-
== Screenshots ==
|
30 |
-
|
31 |
-
1. A look at the configuration dialog for a Category Posts widget.
|
32 |
-
2. You can set how many widgets you want.
|
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.5
|
7 |
+
Stable tag: 1.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:
|
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 ==
|
26 |
|
27 |
1. Download the plugin.
|
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.
|
|
|
|
|
|
|
|
|
|
screenshot-1.png
DELETED
Binary file
|
screenshot-2.png
DELETED
Binary file
|