| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Category Posts Widget |
| 4 |
Plugin URI: http://jameslao.com/2009/07/29/category-posts-widget-2-0/ |
| 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: 2.2 |
| 8 |
Author URI: http://jameslao.com/ |
| 9 |
*/ |
| 10 |
|
| 11 |
class CategoryPosts extends WP_Widget { |
| 12 |
|
| 13 |
function CategoryPosts() { |
| 14 |
parent::WP_Widget(false, $name='Category Posts'); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Displays category posts widget on blog. |
| 19 |
*/ |
| 20 |
function widget($args, $instance) { |
| 21 |
global $post, $wp_query; |
| 22 |
extract( $args ); |
| 23 |
|
| 24 |
// If not title, use the name of the category. |
| 25 |
if( !$instance["title"] ) { |
| 26 |
$category_info = get_category($instance["cat"]); |
| 27 |
$instance["title"] = $category_info->name; |
| 28 |
} |
| 29 |
|
| 30 |
// Save old query... |
| 31 |
$old_query = $wp_query; |
| 32 |
|
| 33 |
// Get array of post info. |
| 34 |
query_posts("showposts=" . $instance["num"] . "&cat=" . $instance["cat"]); |
| 35 |
|
| 36 |
echo $before_widget; |
| 37 |
|
| 38 |
// Widget title |
| 39 |
|
| 40 |
echo $before_title; |
| 41 |
if( (bool) $instance["title_link"] ) |
| 42 |
echo '<a href="' . get_category_link($instance["cat"]) . '">' . $instance["title"] . '</a>'; |
| 43 |
else |
| 44 |
echo $instance["title"]; |
| 45 |
echo $after_title; |
| 46 |
|
| 47 |
// Post list |
| 48 |
echo "<ul>\n"; |
| 49 |
|
| 50 |
while ( have_posts() ) : the_post(); |
| 51 |
?> |
| 52 |
<li class='cat-post-item'> |
| 53 |
|
| 54 |
<?php if ( (bool) $instance["thumbnail"] && function_exists('p75HasThumbnail') && p75HasThumbnail($post->ID) ) { ?> |
| 55 |
|
| 56 |
<a href="<?php the_permalink(); ?>" title="<?php echo the_title_attribute(); ?>"><img class='alignleft' src='<?php echo p75GetThumbnail($post->ID, $instance["thumbnail_width"], $instance["thumbnail_height"]); ?>' alt='<?php echo the_title_attribute(); ?>' /></a> |
| 57 |
|
| 58 |
<?php } // end thumbnail function check ?> |
| 59 |
|
| 60 |
<a class="post-title" href="<?php echo the_permalink(); ?>" title="<?php echo the_title_attribute(); ?>"><?php echo the_title(); ?></a> |
| 61 |
|
| 62 |
<?php if ( $instance['excerpt'] ) the_excerpt(); ?> |
| 63 |
</li> |
| 64 |
<?php |
| 65 |
endwhile; |
| 66 |
|
| 67 |
echo "</ul>\n"; |
| 68 |
|
| 69 |
echo $after_widget; |
| 70 |
|
| 71 |
// Won't even know I was here... |
| 72 |
$wp_query = $old_query; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Form processing... Dead simple. |
| 77 |
*/ |
| 78 |
function update($new_instance, $old_instance) { |
| 79 |
$new_instance["cat"] = absint( $new_instance["cat"] ); |
| 80 |
$new_instance["exclude"] = (bool) $new_instance["exclude"]; |
| 81 |
|
| 82 |
return $new_instance; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* The configuration form. |
| 87 |
*/ |
| 88 |
function form($instance) { |
| 89 |
?> |
| 90 |
<p> |
| 91 |
<label for="<?php echo $this->get_field_id("title"); ?>"> |
| 92 |
<?php _e( 'Title' ); ?>: |
| 93 |
<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"]); ?>" /> |
| 94 |
</label> |
| 95 |
</p> |
| 96 |
|
| 97 |
<p> |
| 98 |
<label> |
| 99 |
<?php _e( 'Category' ); ?>: |
| 100 |
<?php wp_dropdown_categories( array( 'name' => $this->get_field_name("cat"), 'selected' => $instance["cat"] ) ); ?> |
| 101 |
</label> |
| 102 |
</p> |
| 103 |
|
| 104 |
<p> |
| 105 |
<label for="<?php echo $this->get_field_id("num"); ?>"> |
| 106 |
<?php _e('Number of posts to show'); ?>: |
| 107 |
<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' /> |
| 108 |
</label> |
| 109 |
</p> |
| 110 |
|
| 111 |
<p> |
| 112 |
<label for="<?php echo $this->get_field_id("title_link"); ?>"> |
| 113 |
<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 ); ?> /> |
| 114 |
<?php _e( 'Make widget title link' ); ?> |
| 115 |
</label> |
| 116 |
</p> |
| 117 |
|
| 118 |
<p> |
| 119 |
<label for="<?php echo $this->get_field_id("excerpt"); ?>"> |
| 120 |
<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 ); ?> /> |
| 121 |
<?php _e( 'Show post excerpt' ); ?> |
| 122 |
</label> |
| 123 |
</p> |
| 124 |
|
| 125 |
<?php if ( function_exists("p75GetThumbnail") ) : ?> |
| 126 |
<p> |
| 127 |
<label for="<?php echo $this->get_field_id("thumbnail"); ?>"> |
| 128 |
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("thumbnail"); ?>" name="<?php echo $this->get_field_name("thumbnail"); ?>"<?php checked( (bool) $instance["thumbnail"], true ); ?> /> |
| 129 |
<?php _e( 'Show post thumbnail' ); ?> |
| 130 |
</label> |
| 131 |
</p> |
| 132 |
<p> |
| 133 |
<label> |
| 134 |
<?php _e('Thumbnail dimensions'); ?>:<br /> |
| 135 |
<label for="<?php echo $this->get_field_id("thumbnail_width"); ?>"> |
| 136 |
W: <input class="widefat" style="width:40%;" type="text" id="<?php echo $this->get_field_id("thumbnail_width"); ?>" name="<?php echo $this->get_field_name("thumbnail_width"); ?>" value="<?php echo $instance["thumbnail_width"]; ?>" /> |
| 137 |
</label> |
| 138 |
|
| 139 |
<label for="<?php echo $this->get_field_id("thumbnail_height"); ?>"> |
| 140 |
H: <input class="widefat" style="width:40%;" type="text" id="<?php echo $this->get_field_id("thumbnail_height"); ?>" name="<?php echo $this->get_field_name("thumbnail_height"); ?>" value="<?php echo $instance["thumbnail_height"]; ?>" /> |
| 141 |
</label> |
| 142 |
</label> |
| 143 |
</p> |
| 144 |
<?php endif; ?> |
| 145 |
|
| 146 |
<?php |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
add_action( 'widgets_init', create_function('', 'return register_widget("CategoryPosts");') ); |
| 153 |
|
| 154 |
?> |
| 155 |
|