PluginProbe
Category Posts Block / 1.2
Category Posts Block v1.2
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 1.2, at cat-posts.php

116 lines 4.7 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/
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.2
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 $catID = empty($options[$number]['cat']) ? 1 : $options[$number]['cat'];
16
17 // If not title, use the name of the category.
18 if( empty($options[$number]['title']) ) {
19 $categoryInfo = get_category($catID);
20 $title = $categoryInfo->name;
21 } else {
22 $title = $options[$number]['title'];
23 }
24
25 $num = $options[$number]['num'] > 15 ? 15 : $options[$number]['num'];
26
27 echo $before_widget;
28 echo $before_title . $title . $after_title;
29 echo '<ul>';
30 nk_cat_posts($catID, $num);
31 echo '</ul>';
32 echo $after_widget;
33 }
34
35 // The control dialog.
36 function nk_cat_posts_widget_control($number) {
37 $options = $newoptions = get_option('widget_cat_posts');
38 if ( $_POST["cat-posts-title-" . $number] || $_POST["show-cat-id-" . $number] || $_POST["cat-posts-num-" . $number] ) {
39 $newoptions[$number]['title'] = strip_tags(stripslashes($_POST["cat-posts-title-" . $number]));
40 $newoptions[$number]['cat'] = $_POST["show-cat-id-" . $number];
41 $newoptions[$number]['num'] = is_numeric($_POST["cat-posts-num-" . $number]) && $_POST["cat-posts-num-" . $number]!=0 ? $_POST["cat-posts-num-" . $number] : 5;
42 }
43 if ( $options != $newoptions ) {
44 $options = $newoptions;
45 update_option('widget_cat_posts', $options);
46 }
47
48 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>';
49 echo '<p><label>Show posts in ';
50 wp_dropdown_categories(array('name'=>'show-cat-id-' . $number, 'selected'=>$options[$number]['cat']));
51 echo '</label></p>';
52 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>';
53 }
54
55 // Displays the dialog to set how many widgets.
56 function nk_cat_posts_widget_page() {
57 $options = $newoptions = get_option('widget_cat_posts');
58 ?>
59 <div class="wrap">
60 <form method="post">
61 <h2>Category Posts Widgets</h2>
62 <p style="line-height: 30px;">How many category posts widgets would you like?
63 <select id="cat-posts-number" name="cat-posts-number" value="<?php echo $options['num_of_widgets']; ?>">
64 <?php for ( $i = 1; $i <= 20; ++$i ) echo "<option value='$i' ".($options['num_of_widgets']==$i ? "selected='selected'" : '').">$i</option>"; ?>
65 </select>
66 <span class="submit"><input type="submit" name="cat-posts-number-submit" id="cat-posts-number-submit" value="<?php echo attribute_escape(__('Save')); ?>" /></span></p>
67 </form>
68 </div>
69 <?php
70 }
71
72 // Saves how many widgets to be displayed.
73 function nk_cat_posts_widget_setup() {
74 $options = $newoptions = get_option('widget_cat_posts');
75 if ( isset($_POST['cat-posts-number-submit']) ) {
76 $number = (int) $_POST['cat-posts-number'];
77 if ( $number > 20 ) $number = 20;
78 if ( $number < 1 ) $number = 1;
79 $newoptions['num_of_widgets'] = $number;
80 }
81 if ( $options != $newoptions ) {
82 $options = $newoptions;
83 update_option('widget_cat_posts', $options);
84 nk_cat_posts_widget_register($options['num_of_widgets']);
85 }
86 }
87
88 // Registers the widget(s).
89 function nk_cat_posts_widget_register() {
90 $options = get_option('widget_cat_posts');
91 $num_of_widgets = $options['num_of_widgets'];
92 if ( $num_of_widgets < 1 ) $num_of_widgets = 1;
93 if ( $num_of_widgets > 20 ) $num_of_widgets = 20;
94 $dims = array('width' => 300, 'height' => 150);
95 $class = array('classname' => 'widget_cat_posts');
96 for ($i = 1; $i <= 20; $i++) {
97 $name = sprintf('Category Posts %d', $i);
98 $id = "cat-posts-$i";
99 wp_register_sidebar_widget($id, $name, $i <= $num_of_widgets ? 'nk_cat_posts_widget' : '', $class, $i);
100 wp_register_widget_control($id, $name, $i <= $num_of_widgets ? 'nk_cat_posts_widget_control' : '', $dims, $i);
101 }
102 add_action('sidebar_admin_setup', 'nk_cat_posts_widget_setup');
103 add_action('sidebar_admin_page', 'nk_cat_posts_widget_page');
104 }
105
106 function nk_cat_posts($cat, $num = 5) {
107 $catposts = get_posts('numberposts='.$num.'&category='.$cat);
108
109 foreach($catposts as $post) {
110 echo '<li><a href="'.get_permalink($post).'">'.$post->post_title.'</a></li>';
111 }
112 }
113
114 add_action('widgets_init', 'nk_cat_posts_widget_register');
115
116 ?>