Advertisement
alchymyth

3 loops, no duplicates

Jun 19th, 2011
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. First loop (I show the last 3 sticky posts)
  2.  
  3. <?php $do_not_duplicate = array();
  4. $my_query = new WP_Query(array('posts_per_page' => 3, 'post__in' => get_option( 'sticky_posts' ), 'caller_get_posts' => 1 ) );
  5. while ($my_query->have_posts()) : $my_query->the_post();
  6. $do_not_duplicate[] = $post->ID; ?>
  7.  
  8. HTML CODE HERE
  9.  
  10. <?php endwhile;?>
  11. <?php wp_reset_query(); ?>
  12.  
  13. Second loop (the 4 latest posts, excluding stickies)
  14.  
  15. <?php ////$do_not_duplicate = array();////remove this; this line is only allowed once before the first loop
  16. $my_query = new WP_Query(array('posts_per_page' => 4, 'post__not_in' => array_merge($do_not_duplicate, get_option( 'sticky_posts' ) ) ) );
  17. while ($my_query->have_posts()) : $my_query->the_post();
  18. $do_not_duplicate[] = $post->ID; ?>
  19.  
  20. HTML CODE HERE
  21.  
  22. <?php endwhile;?>
  23. <?php wp_reset_query(); ?>
  24.  
  25. Third loop (showing the latest post from each cateogry, excluding stickies)
  26.  
  27. <?php
  28. $cat_args=array(
  29. 'orderby' => 'name',
  30. 'order' => 'ASC'
  31. );
  32.  
  33. ////$do_not_duplicate = array();////remove this; this line is only allowed once before the first loop
  34.  
  35. $categories=get_categories($cat_args);
  36. foreach($categories as $category) {
  37. $args=array(
  38. 'posts_per_page' => 1,
  39. 'post__not_in' => array_merge($do_not_duplicate,get_option( 'sticky_posts' )),
  40. 'category__in' => array($category->term_id),
  41. );
  42. $posts=get_posts($args);
  43. if ($posts) {
  44. $ncat = '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
  45. foreach($posts as $post) {
  46. setup_postdata($post);
  47. $do_not_duplicate[] = $post->ID; ?>
  48.  
  49. HTML CODE HERE
  50.  
  51. <?php
  52. }
  53. }
  54. }
  55. ?>
  56.  
  57. <?php wp_reset_query(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement