| 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.1 |
| 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 |
$titleLink = empty($options[$number]['titleLink']) ? FALSE : $options[$number]['titleLink']; |
| 17 |
|
| 18 |
// If not title, use the name of the category. |
| 19 |
if( empty($options[$number]['title']) ) { |
| 20 |
$categoryInfo = get_category($catID); |
| 21 |
$title = $categoryInfo->name; |
| 22 |
} else { |
| 23 |
$title = $options[$number]['title']; |
| 24 |
} |
| 25 |
|
| 26 |
$num = $options[$number]['num'] > 15 ? 15 : $options[$number]['num']; |
| 27 |
|
| 28 |
echo $before_widget; |
| 29 |
echo $before_title; |
| 30 |
|
| 31 |
if( $titleLink ) { |
| 32 |
echo '<a href="' . get_category_link($catID) . '">' . $title . '</a>'; |
| 33 |
} else { |
| 34 |
echo $title; |
| 35 |
} |
| 36 |
|
| 37 |
echo $after_title; |
| 38 |
echo '<ul>'; |
| 39 |
nk_cat_posts($catID, $num); |
| 40 |
echo '</ul>'; |
| 41 |
echo $after_widget; |
| 42 |
} |
| 43 |
|
| 44 |
// The control dialog. |
| 45 |
function nk_cat_posts_widget_control($number) { |
| 46 |
$options = $newoptions = get_option('widget_cat_posts'); |
| 47 |
if ( $_POST["cat-posts-title-" . $number] || $_POST["show-cat-id-" . $number] || $_POST["cat-posts-num-" . $number] ) { |
| 48 |
$newoptions[$number]['title'] = strip_tags(stripslashes($_POST["cat-posts-title-" . $number])); |
| 49 |
$newoptions[$number]['cat'] = $_POST["show-cat-id-" . $number]; |
| 50 |
$newoptions[$number]['num'] = is_numeric($_POST["cat-posts-num-" . $number]) && $_POST["cat-posts-num-" . $number]!=0 ? $_POST["cat-posts-num-" . $number] : 5; |
| 51 |
$newoptions[$number]['titleLink'] = $_POST["cat-posts-title-link-" . $number] ? TRUE : FALSE; |
| 52 |
} |
| 53 |
if ( $options != $newoptions ) { |
| 54 |
$options = $newoptions; |
| 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 |
echo '<p><label for="cat-posts-title-link-' . $number . '">Link widget title to category page? <input name="cat-posts-title-link-' . $number . '" type="checkbox"'; |
| 65 |
echo $options[$number]['titleLink'] ? ' checked="checked"></label></p>' : '></label></p>'; |
| 66 |
} |
| 67 |
|
| 68 |
// Displays the dialog to set how many widgets. |
| 69 |
function nk_cat_posts_widget_page() { |
| 70 |
$options = $newoptions = get_option('widget_cat_posts'); |
| 71 |
?> |
| 72 |
<div class="wrap"> |
| 73 |
<form method="post"> |
| 74 |
<h2>Category Posts Widgets</h2> |
| 75 |
<p style="line-height: 30px;">How many category posts widgets would you like? |
| 76 |
<select id="cat-posts-number" name="cat-posts-number" value="<?php echo $options['num_of_widgets']; ?>"> |
| 77 |
<?php for ( $i = 1; $i <= 20; ++$i ) echo "<option value='$i' ".($options['num_of_widgets']==$i ? "selected='selected'" : '').">$i</option>"; ?> |
| 78 |
</select> |
| 79 |
<span class="submit"><input type="submit" name="cat-posts-number-submit" id="cat-posts-number-submit" value="<?php echo attribute_escape(__('Save')); ?>" /></span></p> |
| 80 |
</form> |
| 81 |
</div> |
| 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 the widget(s). |
| 102 |
function nk_cat_posts_widget_register() { |
| 103 |
$options = get_option('widget_cat_posts'); |
| 104 |
$num_of_widgets = $options['num_of_widgets']; |
| 105 |
if ( $num_of_widgets < 1 ) $num_of_widgets = 1; |
| 106 |
if ( $num_of_widgets > 20 ) $num_of_widgets = 20; |
| 107 |
$dims = array('width' => 300, 'height' => 160); |
| 108 |
$class = array('classname' => 'widget_cat_posts'); |
| 109 |
for ($i = 1; $i <= 20; $i++) { |
| 110 |
$name = sprintf('Category Posts %d', $i); |
| 111 |
$id = "cat-posts-$i"; |
| 112 |
wp_register_sidebar_widget($id, $name, $i <= $num_of_widgets ? 'nk_cat_posts_widget' : '', $class, $i); |
| 113 |
wp_register_widget_control($id, $name, $i <= $num_of_widgets ? 'nk_cat_posts_widget_control' : '', $dims, $i); |
| 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 |
function nk_cat_posts($cat, $num = 5) { |
| 120 |
$catposts = get_posts('numberposts='.$num.'&category='.$cat); |
| 121 |
|
| 122 |
foreach($catposts as $post) { |
| 123 |
echo '<li><a href="'.get_permalink($post).'">'.$post->post_title.'</a></li>'; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
add_action('widgets_init', 'nk_cat_posts_widget_register'); |
| 128 |
|
| 129 |
?> |