Support » Plugin: Recent Posts by Category Widget » Add feature : exclude the selected category

  • Hi,

    Your plugin is very good because it is light and very simple. I propose to add the reverse mode : You can display a specific category, why not displaying all except this specific category ?

    Here is the modified code.

    Regards

    
    function form( $instance ) {
    
    	$defaults  = array( 'title' => '', 'category' => '', 'number' => 5, 'show_date' => '', 'exclude' => '' );
    	$instance  = wp_parse_args( ( array ) $instance, $defaults );
    	$title     = $instance['title'];
    	$category  = $instance['category'];
    	$number    = $instance['number'];
    	$show_date = $instance['show_date'];
    	$exclude   = $instance['exclude'];
    	
    	?>
    	
    	<p>
    		<label for="rpjc_widget_cat_recent_posts_title"><?php _e( 'Title' ); ?>:</label>
    		<input type="text" class="widefat" id="rpjc_widget_cat_recent_posts_title" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" />
    	</p>
    	
    	<p>
    		<label for="rpjc_widget_cat_recent_posts_category"><?php _e( 'Category' ); ?>:</label>				
    		
    		<?php
    
    		wp_dropdown_categories( array(
    
    			'orderby'    => 'title',
    			'hide_empty' => false,
    			'name'       => $this->get_field_name( 'category' ),
    			'id'         => 'rpjc_widget_cat_recent_posts_category',
    			'class'      => 'widefat',
    			'selected'   => $category
    
    		) );
    
    		?>
    
    	</p>
    	
    	<p>
    		<input type="checkbox" id="rpjc_widget_cat_recent_posts_exclude" class="checkbox" name="<?php echo $this->get_field_name( 'exclude' ); ?>" <?php checked( $exclude, 1 ); ?> />
    		<label for="rpjc_widget_cat_recent_posts_exclude"><?php _e( 'Exclude selected category (reverse mode) ?' ); ?></label>
    	</p>
    
    	<p>
    		<label for="rpjc_widget_cat_recent_posts_number"><?php _e( 'Number of posts to show' ); ?>: </label>
    		<input type="text" id="rpjc_widget_cat_recent_posts_number" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo esc_attr( $number ); ?>" size="3" />
    	</p>
    
    	<p>
    		<input type="checkbox" id="rpjc_widget_cat_recent_posts_show_date" class="checkbox" name="<?php echo $this->get_field_name( 'show_date' ); ?>" <?php checked( $show_date, 1 ); ?> />
    		<label for="rpjc_widget_cat_recent_posts_show_date"><?php _e( 'Display post date?' ); ?></label>
    	</p>
    	
    	<?php
    
    }
    
    // Save widget settings
    
    function update( $new_instance, $old_instance ) {
    
    	$instance              = $old_instance;
    	$instance['title']     = wp_strip_all_tags( $new_instance['title'] );
    	$instance['category']  = wp_strip_all_tags( $new_instance['category'] );
    	$instance['number']    = is_numeric( $new_instance['number'] ) ? intval( $new_instance['number'] ) : 5;
    	$instance['show_date'] = isset( $new_instance['show_date'] ) ? 1 : 0;
    	$instance['exclude']   = isset( $new_instance['exclude'] ) ? 1 : 0;
    
    	return $instance;
    
    }
    
    // Display widget
    
    function widget( $args, $instance ) {
    
    	extract( $args );
    
    	echo $before_widget;
    
    	$title     = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
    	$category  = $instance['category'];
    	$number    = $instance['number'];
    	$show_date = ( $instance['show_date'] === 1 ) ? true : false;
    	$exclude   = ( $instance['exclude'] === 1 ) ? true : false;
    
    	if ( !empty( $title ) ) echo $before_title . $title . $after_title;
    
    	$query = array( 
    		'post_type'      => 'post',
    		'posts_per_page' => $number
    		);
    	if(!$exclude) $query['cat'] = $category;
    	else $query['category__not_in'] = $category;
    	
    	$cat_recent_posts = new WP_Query($query);
    
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Add feature : exclude the selected category’ is closed to new replies.