Advertisement
Guest User

Wordpress Recent Posts widget w/ category exclude

a guest
Jun 21st, 2012
18,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. /**
  2. * Recent_Posts widget w/ category exclude class
  3. * This allows specific Category IDs to be removed from the Sidebar Recent Posts list
  4. *
  5. */
  6. class WP_Widget_Recent_Posts_Exclude extends WP_Widget {
  7.  
  8. function __construct() {
  9. $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your site") );
  10. parent::__construct('recent-posts', __('Recent Posts with Exclude'), $widget_ops);
  11. $this->alt_option_name = 'widget_recent_entries';
  12.  
  13. add_action( 'save_post', array(&$this, 'flush_widget_cache') );
  14. add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
  15. add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
  16. }
  17.  
  18. function widget($args, $instance) {
  19. $cache = wp_cache_get('widget_recent_posts', 'widget');
  20.  
  21. if ( !is_array($cache) )
  22. $cache = array();
  23.  
  24. if ( ! isset( $args['widget_id'] ) )
  25. $args['widget_id'] = $this->id;
  26.  
  27. if ( isset( $cache[ $args['widget_id'] ] ) ) {
  28. echo $cache[ $args['widget_id'] ];
  29. return;
  30. }
  31.  
  32. ob_start();
  33. extract($args);
  34.  
  35. $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title'], $instance, $this->id_base);
  36. if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
  37. $number = 10;
  38. $exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude'];
  39.  
  40. $r = new WP_Query(array('posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true, 'category__not_in' => explode(',', $exclude) ));
  41. if ($r->have_posts()) :
  42. ?>
  43. <?php //echo print_r(explode(',', $exclude)); ?>
  44. <?php echo $before_widget; ?>
  45. <?php if ( $title ) echo $before_title . $title . $after_title; ?>
  46. <ul>
  47. <?php while ($r->have_posts()) : $r->the_post(); ?>
  48. <li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a></li>
  49. <?php endwhile; ?>
  50. </ul>
  51. <?php echo $after_widget; ?>
  52. <?php
  53. // Reset the global $the_post as this query will have stomped on it
  54. wp_reset_postdata();
  55.  
  56. endif;
  57.  
  58. $cache[$args['widget_id']] = ob_get_flush();
  59. wp_cache_set('widget_recent_posts', $cache, 'widget');
  60. }
  61.  
  62. function update( $new_instance, $old_instance ) {
  63. $instance = $old_instance;
  64. $instance['title'] = strip_tags($new_instance['title']);
  65. $instance['number'] = (int) $new_instance['number'];
  66. $instance['exclude'] = strip_tags( $new_instance['exclude'] );
  67. $this->flush_widget_cache();
  68.  
  69. $alloptions = wp_cache_get( 'alloptions', 'options' );
  70. if ( isset($alloptions['widget_recent_entries']) )
  71. delete_option('widget_recent_entries');
  72.  
  73. return $instance;
  74. }
  75.  
  76. function flush_widget_cache() {
  77. wp_cache_delete('widget_recent_posts', 'widget');
  78. }
  79.  
  80. function form( $instance ) {
  81. $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
  82. $number = isset($instance['number']) ? absint($instance['number']) : 5;
  83. $exclude = esc_attr( $instance['exclude'] );
  84. ?>
  85. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  86. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
  87.  
  88. <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label>
  89. <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
  90.  
  91. <p>
  92. <label for="<?php echo $this->get_field_id('exclude'); ?>"><?php _e( 'Exclude Category(s):' ); ?></label> <input type="text" value="<?php echo $exclude; ?>" name="<?php echo $this->get_field_name('exclude'); ?>" id="<?php echo $this->get_field_id('exclude'); ?>" class="widefat" />
  93. <br />
  94. <small><?php _e( 'Category IDs, separated by commas.' ); ?></small>
  95. </p>
  96. <?php
  97. }
  98. }
  99.  
  100. function WP_Widget_Recent_Posts_Exclude_init() {
  101. unregister_widget('WP_Widget_Recent_Posts');
  102. register_widget('WP_Widget_Recent_Posts_Exclude');
  103. }
  104.  
  105. add_action('widgets_init', 'WP_Widget_Recent_Posts_Exclude_init');
  106.  
  107. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement