$size ) {
add_image_size( 'cat_post_thumb_size' . $id, $size[0], $size[1], true );
}
}
}
add_action( 'init', 'category_posts_add_image_size' );
/**
* Category Posts Widget Class
*
* Shows the single category posts with some configurable options
*/
class CategoryPosts extends WP_Widget {
function CategoryPosts() {
$widget_ops = array('classname' => 'cat-post-widget', 'description' => __('List single category posts for your Bloggr sidebars', 'bloggr' ));
$this->WP_Widget('category-posts', __('MT: Sidebar Category Posts', 'bloggr' ), $widget_ops);
}
// Displays category posts widget on blog.
function widget($args, $instance) {
global $post;
$post_old = $post; // Save the post object.
extract( $args );
$sizes = get_option('mkrdip_cat_post_thumb_sizes');
// If not title, use the name of the category.
if( !$instance["title"] ) {
$category_info = get_category($instance["cat"]);
$instance["title"] = $category_info->name;
}
$valid_sort_orders = array('date', 'title', 'comment_count', 'rand');
if ( in_array($instance['sort_by'], $valid_sort_orders) ) {
$sort_by = $instance['sort_by'];
$sort_order = (bool) isset( $instance['asc_sort_order'] ) ? 'ASC' : 'DESC';
} else {
// by default, display latest first
$sort_by = 'date';
$sort_order = 'DESC';
}
// Get array of post info.
$cat_posts = new WP_Query(
"showposts=" . $instance["num"] .
"&cat=" . $instance["cat"] .
"&orderby=" . $sort_by .
"&order=" . $sort_order
);
// Excerpt length filter
$new_excerpt_length = create_function('$length', "return " . $instance["excerpt_length"] . ";");
if ( $instance["excerpt_length"] > 0 )
add_filter('excerpt_length', $new_excerpt_length);
echo $before_widget;
// Widget title
echo $before_title;
if( isset( $instance["title_link"] ) )
echo '' . $instance["title"] . '';
else
echo $instance["title"];
echo $after_title;
// Post list
echo "
\n";
while ( $cat_posts->have_posts() )
{
$cat_posts->the_post();
?>
-
id ); ?>
\n";
echo $after_widget;
remove_filter('excerpt_length', $new_excerpt_length);
wp_reset_postdata();
}
/**
* Update the options
*
* @param array $new_instance
* @param array $old_instance
* @return array
*/
function update($new_instance, $old_instance) {
$sizes = get_option('mkrdip_cat_post_thumb_sizes');
if ( !$sizes ) {
$sizes = array();
}
$sizes[$this->id] = array($new_instance['thumb_w'], $new_instance['thumb_h']);
update_option('mkrdip_cat_post_thumb_sizes', $sizes);
return $new_instance;
}
/**
* The widget configuration form back end.
*
* @param array $instance
* @return void
*/
function form($instance) {
$instance = wp_parse_args( ( array ) $instance, array(
'title' => __( '', 'bloggr' ),
'cat' => __( '', 'bloggr' ),
'num' => __( '', 'bloggr' ),
'sort_by' => __( '', 'bloggr' ),
'asc_sort_order' => __( '', 'bloggr' ),
'title_link' => __( '', 'bloggr' ),
'excerpt' => __( '', 'bloggr' ),
'excerpt_length' => __( '', 'bloggr' ),
'comment_num' => __( '', 'bloggr' ),
'date' => __( '', 'bloggr' ),
'thumb' => __( '', 'bloggr' ),
'thumb_w' => __( '', 'bloggr' ),
'thumb_h' => __( '', 'bloggr' )
) );
$title = $instance['title'];
$cat = $instance['cat'];
$num = $instance['num'];
$sort_by = $instance['sort_by'];
$asc_sort_order = $instance['asc_sort_order'];
$title_link = $instance['title_link'];
$excerpt = $instance['excerpt'];
$excerpt_length = $instance['excerpt_length'];
$comment_num = $instance['comment_num'];
$date = $instance['date'];
$thumb = $instance['thumb'];
$thumb_w = $instance['thumb_w'];
$thumb_h = $instance['thumb_h'];
?>
" name="get_field_name("excerpt_length"); ?>" value="" size="3" />