Exclude Featured Posts from WPZOOM: Featured Category Widget

Zenko WPZOOM Demo Site (see the documentation)
I made customization to the widget to exclude Same Posts already been shown on Featured Slider to avoid duplication

1. Open featured-category.php in wp-content/themes/zenko/functions/theme/widgets folder

2. Replace all code in featured-category.php with the following


<?php /*------------------------------------------*/ /* WPZOOM: Featured Category widget */ /*------------------------------------------*/ $wpzoomColors = array(); $wpzoomColors['blue'] = 'Blue'; $wpzoomColors['green'] = 'Green'; $wpzoomColors['pink'] = 'Pink'; $wpzoomColors['black'] = 'Black'; class wpzoom_widget_category extends WP_Widget { /* Widget setup. */  function wpzoom_widget_category() {  /* Widget settings. */  $widget_ops = array( 'classname' =--> 'wpzoom', 'description' => __('Featured Category Widget for Homepage', 'wpzoom') );

/* Widget control settings. */
 $control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'wpzoom-widget-cat' );

/* Create the widget. */
 $this->WP_Widget( 'wpzoom-widget-cat', __('WPZOOM: Featured Category', 'wpzoom'), $widget_ops, $control_ops );
 }

/* How to display the widget on the screen. */
 function widget( $args, $instance ) {
 extract( $args );

/* Our variables from the widget settings. */
 $title1 = apply_filters('widget_title', $instance['title1'] );
 $color1 = $instance['color1'];
 $posts1 = $instance['posts1'];
 $category1 = get_category($instance['category1']);
 if ($category1) {
 $categoryLink1 = get_category_link($category1);
 }

/* Before widget (defined by themes). */
 //echo $before_widget;

$z = 0;
 while ($z < 1)  {  $z++; $color = "color$z";  $categoryLink = "categoryLink$z";  $title = "title$z";  $posts = "posts$z";  $category = $instance["category$z"]; ?></pre>
<div class="block"><!--?php if ( $$title ) { echo '<br ?-->
<h3 class="'.$$color.'"><a href="'.$$categoryLink.'">'.$$title.' »</a></h3>
 '; } ?>

<?php  $featured_posts = new WP_Query(  array(  'post__not_in' =--> get_option( 'sticky_posts' ),
 'meta_key' => 'wpzoom_is_featured',
 'meta_value' => 1
 ) );

 while ($featured_posts->have_posts()) {
 $featured_posts->the_post();
 global $post;
 $postIDs[] = $post->ID;
 }
 $args=array( 'cat' => $category, 'post__not_in' => $postIDs, 'showposts' => $$posts, 'orderby' => 'date', 'order' => 'DESC' );
 $second_query = new WP_Query( $args );

$i = 0;
 if ( $second_query->have_posts() ) : while( $second_query->have_posts() ) : $second_query->the_post();
 unset($image,$cropLocation);
 $i++;
 global $post;

if ($i == 1)
 { ?>
<div class="firstn">
<?php get_the_image( array( 'size' =--> 'featured-cat', 'width' => 235, 'height' => 140 ) ); ?>
<h2></h2>
<small><?php printf('%s at %s', get_the_date(), get_the_time()); ?></small>

<?php the_content_limit(170); ?></div>
<!-- A list with another 3 articles from the same category-->
<div class="rightn">
<ul>
<ul>
<li><?php get_the_image( array( 'size' =--> 'featured-cat-small', 'width' => 90, 'height' => 80 ) ); ?>
<div class="rightcont">
<div class="bubble"></div>
<?php the_content_limit(110, ''); ?>

<small><?php printf('%s at %s', get_the_date(), get_the_time()); ?></small></div>
<div style="clear: both;"></div></li>
</ul>
</ul>
 <?php else :
 echo'';
 _e('There are no posts in this category.', 'wpzoom');
 echo'

';
 endif;
 ?></div>
<div class="clear"></div>
</div>
<pre>
<!-- end .category -->
<!--?php <br ?-->
} // while
 echo ' <!-- end .featCategory -->';
 /* After widget (defined by themes). */
 //echo $after_widget;
 wp_reset_query();
 }

/* Update the widget settings.*/
 function update( $new_instance, $old_instance ) {
 $instance = $old_instance;

/* Strip tags for title and name to remove HTML (important for text inputs). */
 $instance['title1'] = strip_tags( $new_instance['title1'] );
 $instance['category1'] = $new_instance['category1'];
 $instance['color1'] = $new_instance['color1'];
 $instance['posts1'] = $new_instance['posts1'];

return $instance;
 }

/** Displays the widget settings controls on the widget panel.
 * Make use of the get_field_id() and get_field_name() function when creating your form elements. This handles the confusing stuff. */
 function form( $instance ) {

/* Set up some default widget settings. */
 $defaults = array( 'title1' => __('Category Name', 'wpzoom'), 'category1' => '0', 'color1' => 'blue', 'posts1' => '4' );
 $instance = wp_parse_args( (array) $instance, $defaults );
 global $wpzoomColors;
 ?>

<label for="<?php echo $this->get_field_id( 'title1' ); ?>"><?php _e('Category Title:', 'wpzoom'); ?></label>
 <input id="<?php echo $this->get_field_id( 'title1' ); ?>" class="widefat" type="text" name="<?php echo $this->get_field_name( 'title1' ); ?>" value="<?php echo $instance['title1']; ?>" />

<label for="<?php echo $this->get_field_id('color1'); ?>"><?php _e('Title Background Color:', 'wpzoom'); ?></label>

<select style="width: 90%;" name="<?php echo $this->get_field_name('color1'); ?>"> <?php  foreach ($wpzoomColors as $key =--> $value) { $option = '<option selected="selected" value="'.$key;  if ($key == $instance['color1']) { $option .='">';</option></select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('color1'); ?>"><option selected="selected" value="'.$key;  if ($key == $instance['color1']) { $option .='"> $option .= $value;</option></select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('color1'); ?>"><option selected="selected" value="'.$key;  if ($key == $instance['color1']) { $option .='"> $option .= '</option>';</select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('color1'); ?>"> echo $option;</select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('color1'); ?>"> }</select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('color1'); ?>"> ?></select>


<label for="<?php echo $this->get_field_id('category1'); ?>"><?php _e('Category:', 'wpzoom'); ?></label>

<select style="width: 90%;" name="<?php echo $this->get_field_name('category1'); ?>"> <option value="0">Choose category:</option></select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('category1'); ?>"> <?php <br /> ?> $cats = get_categories('hide_empty=0');</select>

<select style="width: 90%;" name="<?php echo $this->get_field_name('category1'); ?>">foreach ($cats as $cat) {</select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('category1'); ?>"> $option = '<option selected="selected" value="'.$cat->term_id;<br /> if ($cat->term_id == $instance['category1']) { $option .='">';</option></select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('category1'); ?>"><option selected="selected" value="'.$cat->term_id;<br /> if ($cat->term_id == $instance['category1']) { $option .='"> $option .= $cat->cat_name;</option></select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('category1'); ?>"><option selected="selected" value="'.$cat->term_id;<br /> if ($cat->term_id == $instance['category1']) { $option .='"> $option .= ' ('.$cat->category_count.')';</option></select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('category1'); ?>"><option selected="selected" value="'.$cat->term_id;<br /> if ($cat->term_id == $instance['category1']) { $option .='"> $option .= '</option>';</select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('category1'); ?>"> echo $option;</select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('category1'); ?>"> }</select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('category1'); ?>"> ?></select>


<label for="<?php echo $this->get_field_id('posts1'); ?>"><?php _e('Posts to show:', 'wpzoom'); ?></label>

<select style="width: 90%;" name="<?php echo $this->get_field_name('posts1'); ?>"> <?php <br ?> $m = 0;</select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('posts1'); ?>"> while ($m < 11) {</select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('posts1'); ?>"> $m++;</select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('posts1'); ?>"> $option = '<option selected="selected" value="'.$m;  if ($m == $instance['posts1']) { $option .='">';</option></select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('posts1'); ?>"><option selected="selected" value="'.$m;  if ($m == $instance['posts1']) { $option .='"> $option .= $m;</option></select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('posts1'); ?>"><option selected="selected" value="'.$m;  if ($m == $instance['posts1']) { $option .='"> $option .= '</option>';</select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('posts1'); ?>"> echo $option;</select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('posts1'); ?>"> }</select>
<select style="width: 90%;" name="<?php echo $this->get_field_name('posts1'); ?>"> ?></select>


<?php  } } function wpzoom_register_category_widget() {  register_widget('wpzoom_widget_category'); } add_action('widgets_init', 'wpzoom_register_category_widget'); ?>


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: