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

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