All pastes #805956 Raw Edit

recent posts by category widget

public php v1 · immutable
·published 2007-12-04 20:43 UTC
rendered paste body
<?php/*Plugin Name: Recent posts by category widgetDescription: Adds a sidebar widget to show the most recent posts sorted by categoryAuthor: Nef GarciaVersion: .00001*/// Put functions into one big function we'll call at the plugins_loaded// action. This ensures that all required plugin functions are defined.function widget_recent_entries_by_category_init() {	// Check for the required plugin functions. This will prevent fatal	// errors occurring when you deactivate the dynamic-sidebar plugin.	if ( !function_exists('register_sidebar_widget') )		return;	function wp_widget_recent_entries_by_category($args) {		if ( $output = wp_cache_get('widget_recent_entries_by_category') )			return print($output);			ob_start();		extract($args);		$options = get_option('widget_recent_entries_by_category');		$title = empty($options['title']) ? __('Recent Posts') : $options['title'];		if ( !$number = (int) $options['number'] )			$number = 10;		else if ( $number < 1 )			$number = 1;		else if ( $number > 15 )			$number = 15;		$category = $options['category'];			$r = new WP_Query("category_name='$category'&showposts=$number&what_to_show=posts&nopaging=0&post_status=publish");		if ($r->have_posts()) :	?>			<?php echo $before_widget; ?>				<?php echo $before_title . $title . $after_title; ?>				<ul>				<?php  while ($r->have_posts()) : $r->the_post(); ?>				<li><a href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li>				<?php endwhile; ?>				</ul>			<?php echo $after_widget; ?>	<?php		endif;		wp_cache_add('widget_recent_entries_by_category', ob_get_flush());	}	function wp_flush_widget_recent_entries_by_category() {		wp_cache_delete('widget_recent_entries_by_category');	}		add_action('save_post', 'wp_flush_widget_recent_entries_by_category');	add_action('deleted_post', 'wp_flush_widget_recent_entries_by_category');	function wp_widget_recent_entries_by_category_control() {		$options = $newoptions = get_option('widget_recent_entries_by_category');		if ( $_POST["recent-entries-by-category-submit"] ) {			$newoptions['title'] = strip_tags(stripslashes($_POST["recent-entries-by-category-title"]));			$newoptions['number'] = (int) $_POST["recent-entries-by-category-number"];			$newoptions['category'] = strip_tags(stripslashes($_POST["recent-entries-by-category-category"]));		}		if ( $options != $newoptions ) {			$options = $newoptions;			update_option('widget_recent_entries_by_category', $options);			wp_flush_widget_recent_entries_by_category();		}		$title = attribute_escape($options['title']);		$category = attribute_escape($options['category']);		if ( !$number = (int) $options['number'] )			$number = 5;?>			<p><label for="recent-entries-by-category-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="recent-entries-by-category-title" name="recent-entries-by-category-title" type="text" value="<?php echo $title; ?>" /></label></p>			<p><label for="recent-entries-by-category-category"><?php _e('Category:'); ?> <input style="width: 250px;" id="recent-entries-by-category-category" name="recent-entries-by-category-category" type="text" value="<?php echo $category; ?>" /></label></p>			<p><label for="recent-entries-by-category-number"><?php _e('Number of posts to show:'); ?> <input style="width: 25px; text-align: center;" id="recent-entries-by-category-number" name="recent-entries-by-category-number" type="text" value="<?php echo $number; ?>" /></label> <?php _e('(at most 15)'); ?></p>			<input type="hidden" id="recent-entries-by-category-submit" name="recent-entries-by-category-submit" value="1" /><?php	}	register_sidebar_widget('Recent posts by category', 'wp_widget_recent_entries_by_category');	register_widget_control('Recent posts by category', 'wp_widget_recent_entries_by_category_control');}add_action('widgets_init', 'widget_recent_entries_by_category_init');?>