do not duplicate posts from widgets
-
I have put my main loop into a widget. I also put a feature slider and feature posts displayed in boxes, both with options to decide which category to set as feature. My problem is that when all of these widgets are displayed on the same page, I don’t want the posts to be duplicated in the main loop.
I am aware that I have to declare a global variable and assign post IDs to that variable in an array, but I am not doing it right, apparently.
Here is the code for the feature slider loop:
global $themename_do_not_duplicate; $themename_do_not_duplicate = array(); $arr = array(); $i=1; $width = $instance['ImgWidth']; $height = $instance['ImgHeight']; $cat_posts = new WP_Query("showposts=" . $instance["featuredNum"] . "&cat=" . $instance["cat"]); while ($cat_posts->have_posts()) : $cat_posts->the_post() $themename_do_not_duplicate[] = $posts->ID;?> <li><a href="<?php the_permalink();?>"> <?php $post_title = get_the_title(); $thumbnail = get_thumbnail($width,$height,'thumb',$post_title,$post_title); $thumb = $thumbnail["thumb"]; print_thumbnail($thumb, $thumbnail["use_timthumb"], $post_title, $width, $height, 'thumb'); ?></a> <div class="title"> <h2><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h2> </div> </li> <?php endwhile; wp_reset_query(); ?>
Here is the loop for the feature boxes, which has four loops:
<?php query_posts("showposts=1&cat=".get_catId($instance['catOption4'])); while (have_posts()) : the_post(); $themename_do_not_duplicate[] = $post->ID;?> ...stufff <?php endwhile; wp_reset_query(); ?>
Do I have to put the
global themename_do_not_duplicate
before each loop here or can I just put it in the beginning of the widget?Here is the main loop:
<?php global $themename_do_not_duplicate; if ($instance['NoRepeatPost'] == 1) {$arrgs=array('posts_per_page'=>$instance['postNum'],'post__not_in' => $themename_do_not_duplicate,'paged'=>$paged,);} else {$arrgs=array('posts_per_page'=>$instance['postNum'],'paged'=>$paged,);} query_posts($arrgs); if(have_posts()) : while (have_posts()) : the_post();$themename_do_not_duplicate[] = $post->ID; //begin the loop ?> ...stuff <?php endif;wp_reset_query(); //and so ends the loop ?>
In the top of the header.php file I put
global themename_do_not_duplicate
I hope I explained my problem thoroughly.
- The topic ‘do not duplicate posts from widgets’ is closed to new replies.