| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Same Category Posts |
| 4 |
Plugin URI: https://wordpress.org/plugins/same-category-posts/ |
| 5 |
Description: Adds a widget that shows the most recent posts from a single category. |
| 6 |
Author: DFlöter |
| 7 |
Version: 1.0.1 |
| 8 |
Author URI: https://profiles.wordpress.org/kometschuh/ |
| 9 |
*/ |
| 10 |
|
| 11 |
// Don't call the file directly |
| 12 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 13 |
|
| 14 |
/** |
| 15 |
* Register our styles |
| 16 |
* |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
add_action( 'wp_enqueue_scripts', 'same_category_posts_styles' ); |
| 20 |
|
| 21 |
function same_category_posts_styles() { |
| 22 |
wp_register_style( 'same-category-posts', plugins_url( 'same-category-posts/same-category-posts.css' ) ); |
| 23 |
wp_enqueue_style( 'same-category-posts' ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Register thumbnail sizes. |
| 28 |
* |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
if ( function_exists('add_image_size') ) |
| 32 |
{ |
| 33 |
$sizes = get_option('kts_same_category_post_thumb_sizes'); |
| 34 |
if ( $sizes ) |
| 35 |
{ |
| 36 |
foreach ( $sizes as $id=>$size ) |
| 37 |
add_image_size( 'related_post_thumb_size' . $id, $size[0], $size[1], true ); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Related Posts Widget Class |
| 43 |
* |
| 44 |
* Shows posts from same category with some configurable options |
| 45 |
*/ |
| 46 |
class SameCategoryPosts extends WP_Widget { |
| 47 |
|
| 48 |
function __construct() { |
| 49 |
$widget_ops = array('classname' => 'same-category-posts', 'description' => __('List posts from same category in sidebar based on shown post\'s category')); |
| 50 |
parent::__construct('same-category-posts', __('Same Category Posts'), $widget_ops); |
| 51 |
} |
| 52 |
|
| 53 |
// Displays a list of posts from same category on single post pages. |
| 54 |
function widget($args, $instance) { |
| 55 |
// Only show widget if on a post page. |
| 56 |
if ( !is_single() ) return; |
| 57 |
|
| 58 |
global $post; |
| 59 |
$post_old = $post; // Save the post object. |
| 60 |
|
| 61 |
extract( $args ); |
| 62 |
|
| 63 |
$categories = get_the_category(); |
| 64 |
$category = $categories[0]->cat_ID; |
| 65 |
|
| 66 |
if( !$instance["title"] ) { |
| 67 |
$category_info = get_category( $category ); |
| 68 |
$instance["title"] = $category_info->name; |
| 69 |
} |
| 70 |
|
| 71 |
// Excerpt length filter |
| 72 |
$new_excerpt_length = create_function('$length', "return " . $instance["excerpt_length"] . ";"); |
| 73 |
if ( $instance["excerpt_length"] > 0 ) |
| 74 |
add_filter('excerpt_length', $new_excerpt_length); |
| 75 |
|
| 76 |
$args=array( |
| 77 |
'cat' => $category . ',' . -$instance['exclude_category'], |
| 78 |
'showposts'=> $instance['num'], // Number of related posts that will be shown. |
| 79 |
'ignore_sticky_posts'=>1 |
| 80 |
); |
| 81 |
$my_query = new WP_Query($args); |
| 82 |
|
| 83 |
if( $my_query->have_posts() ) |
| 84 |
{ |
| 85 |
echo $before_widget; |
| 86 |
|
| 87 |
// Widget title |
| 88 |
echo $before_title . $instance["title"] . $after_title; |
| 89 |
|
| 90 |
// Post list |
| 91 |
echo "<ul>\n"; |
| 92 |
while ($my_query->have_posts()) |
| 93 |
{ |
| 94 |
$my_query->the_post(); |
| 95 |
?> |
| 96 |
<li class="same-category-posts"> |
| 97 |
<a class="post-title" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a> |
| 98 |
|
| 99 |
<?php if ( isset( $instance['date'] ) ) : ?> |
| 100 |
<p class="post-date"><?php the_time("j M Y"); ?></p> |
| 101 |
<?php endif; ?> |
| 102 |
|
| 103 |
<?php |
| 104 |
if ( |
| 105 |
function_exists('the_post_thumbnail') && |
| 106 |
current_theme_supports("post-thumbnails") && |
| 107 |
isset ( $instance["thumb"] ) && |
| 108 |
has_post_thumbnail() |
| 109 |
) : |
| 110 |
?> |
| 111 |
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> |
| 112 |
<?php the_post_thumbnail( 'related_post_thumb_size'.$this->id ); ?> |
| 113 |
</a> |
| 114 |
<?php endif; ?> |
| 115 |
|
| 116 |
<?php if ( isset ( $instance['excerpt'] ) ) : ?> |
| 117 |
<?php the_excerpt(); ?> |
| 118 |
<?php endif; ?> |
| 119 |
|
| 120 |
<?php if ( isset ( $instance['comment_num'] ) ) : ?> |
| 121 |
<p class="comment-num">(<?php comments_number(); ?>)</p> |
| 122 |
<?php endif; ?> |
| 123 |
</li> |
| 124 |
<?php |
| 125 |
} |
| 126 |
echo "</ul>\n"; |
| 127 |
|
| 128 |
echo $after_widget; |
| 129 |
} |
| 130 |
|
| 131 |
remove_filter('excerpt_length', $new_excerpt_length); |
| 132 |
|
| 133 |
$post = $post_old; // Restore the post object. |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Update the options |
| 138 |
* |
| 139 |
* @param array $new_instance |
| 140 |
* @param array $old_instance |
| 141 |
* @return array |
| 142 |
*/ |
| 143 |
function update($new_instance, $old_instance) { |
| 144 |
if ( function_exists('the_post_thumbnail') ) |
| 145 |
{ |
| 146 |
$sizes = get_option('kts_same_category_post_thumb_sizes'); |
| 147 |
if ( !$sizes ) $sizes = array(); |
| 148 |
$sizes[$this->id] = array($new_instance['thumb_w'], $new_instance['thumb_h']); |
| 149 |
update_option('kts_same_category_post_thumb_sizes', $sizes); |
| 150 |
} |
| 151 |
|
| 152 |
return $new_instance; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* The widget configuration form back end. |
| 157 |
* |
| 158 |
* @param array $instance |
| 159 |
* @return void |
| 160 |
*/ |
| 161 |
function form($instance) { |
| 162 |
$instance = wp_parse_args( ( array ) $instance, array( |
| 163 |
'title' => __( '' ), |
| 164 |
'num' => __( '' ), |
| 165 |
'title_link' => __( '' ), |
| 166 |
'exclude_category' => __( '' ), |
| 167 |
'excerpt' => __( '' ), |
| 168 |
'excerpt_length' => __( '' ), |
| 169 |
'comment_num' => __( '' ), |
| 170 |
'date' => __( '' ), |
| 171 |
'thumb' => __( '' ), |
| 172 |
'thumb_w' => __( '' ), |
| 173 |
'thumb_h' => __( '' ) |
| 174 |
) ); |
| 175 |
|
| 176 |
$title = $instance['title']; |
| 177 |
$num = $instance['num']; |
| 178 |
$title_link = $instance['title_link']; |
| 179 |
$exclude_category = $instance['exclude_category']; |
| 180 |
$excerpt = $instance['excerpt']; |
| 181 |
$excerpt_length = $instance['excerpt_length']; |
| 182 |
$comment_num = $instance['comment_num']; |
| 183 |
$date = $instance['date']; |
| 184 |
$thumb = $instance['thumb']; |
| 185 |
$thumb_w = $instance['thumb_w']; |
| 186 |
$thumb_h = $instance['thumb_h']; |
| 187 |
|
| 188 |
?> |
| 189 |
<p> |
| 190 |
<label for="<?php echo $this->get_field_id("title"); ?>"> |
| 191 |
<?php _e( 'Title' ); ?>: |
| 192 |
<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"]); ?>" /> |
| 193 |
</label> |
| 194 |
</p> |
| 195 |
|
| 196 |
<p> |
| 197 |
<label for="<?php echo $this->get_field_id("num"); ?>"> |
| 198 |
<?php _e('Number of posts to show'); ?>: |
| 199 |
<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' /> |
| 200 |
</label> |
| 201 |
</p> |
| 202 |
|
| 203 |
<p> |
| 204 |
<label> |
| 205 |
<?php _e( 'Exclude category' ); ?>: |
| 206 |
<?php wp_dropdown_categories( array( 'show_option_none' => ' ', 'name' => $this->get_field_name("exclude_category"), 'selected' => $instance["exclude_category"] ) ); ?> |
| 207 |
</label> |
| 208 |
</p> |
| 209 |
|
| 210 |
<p> |
| 211 |
<label for="<?php echo $this->get_field_id("excerpt"); ?>"> |
| 212 |
<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 ); ?> /> |
| 213 |
<?php _e( 'Show post excerpt' ); ?> |
| 214 |
</label> |
| 215 |
</p> |
| 216 |
|
| 217 |
<p> |
| 218 |
<label for="<?php echo $this->get_field_id("excerpt_length"); ?>"> |
| 219 |
<?php _e( 'Excerpt length (in words):' ); ?> |
| 220 |
</label> |
| 221 |
<input style="text-align: center;" type="text" id="<?php echo $this->get_field_id("excerpt_length"); ?>" name="<?php echo $this->get_field_name("excerpt_length"); ?>" value="<?php echo $instance["excerpt_length"]; ?>" size="3" /> |
| 222 |
</p> |
| 223 |
|
| 224 |
<p> |
| 225 |
<label for="<?php echo $this->get_field_id("comment_num"); ?>"> |
| 226 |
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("comment_num"); ?>" name="<?php echo $this->get_field_name("comment_num"); ?>"<?php checked( (bool) $instance["comment_num"], true ); ?> /> |
| 227 |
<?php _e( 'Show number of comments' ); ?> |
| 228 |
</label> |
| 229 |
</p> |
| 230 |
|
| 231 |
<p> |
| 232 |
<label for="<?php echo $this->get_field_id("date"); ?>"> |
| 233 |
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("date"); ?>" name="<?php echo $this->get_field_name("date"); ?>"<?php checked( (bool) $instance["date"], true ); ?> /> |
| 234 |
<?php _e( 'Show post date' ); ?> |
| 235 |
</label> |
| 236 |
</p> |
| 237 |
|
| 238 |
<?php if ( function_exists('the_post_thumbnail') && current_theme_supports("post-thumbnails") ) : ?> |
| 239 |
<p> |
| 240 |
<label for="<?php echo $this->get_field_id("thumb"); ?>"> |
| 241 |
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("thumb"); ?>" name="<?php echo $this->get_field_name("thumb"); ?>"<?php checked( (bool) $instance["thumb"], true ); ?> /> |
| 242 |
<?php _e( 'Show post thumbnail' ); ?> |
| 243 |
</label> |
| 244 |
</p> |
| 245 |
<p> |
| 246 |
<label> |
| 247 |
<?php _e('Thumbnail dimensions (in pixels)'); ?>:<br /> |
| 248 |
<label for="<?php echo $this->get_field_id("thumb_w"); ?>"> |
| 249 |
Width: <input class="widefat" style="width:30%;" type="text" id="<?php echo $this->get_field_id("thumb_w"); ?>" name="<?php echo $this->get_field_name("thumb_w"); ?>" value="<?php echo $instance["thumb_w"]; ?>" /> |
| 250 |
</label> |
| 251 |
|
| 252 |
<label for="<?php echo $this->get_field_id("thumb_h"); ?>"> |
| 253 |
Height: <input class="widefat" style="width:30%;" type="text" id="<?php echo $this->get_field_id("thumb_h"); ?>" name="<?php echo $this->get_field_name("thumb_h"); ?>" value="<?php echo $instance["thumb_h"]; ?>" /> |
| 254 |
</label> |
| 255 |
</label> |
| 256 |
</p> |
| 257 |
<?php endif; ?> |
| 258 |
|
| 259 |
<?php |
| 260 |
|
| 261 |
} |
| 262 |
|
| 263 |
} |
| 264 |
|
| 265 |
add_action( 'widgets_init', create_function('', 'return register_widget("SameCategoryPosts");') ); |
| 266 |
|