Posted By

gd6d on 11/30/09


Tagged

wordpress loops


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

jlan


Using Multiple Loops without Duplication


 / Published in: PHP
 

URL: http://www.blogohblog.com/10-more-wordpress-hacks-for-easy-life/

  1. If you need to break display your posts in different sections (like in Magazine themes), where you want to display a fixed number of posts first, and the remaining later, you can use this code to make multiple loops (with offsetting the fixed posts in the later loop).
  2.  
  3. The first loop that will pull and display 5 recent posts :-
  4. view source
  5. print?
  6. 1 <?php
  7. 2 query_posts('showposts=5');
  8. 3 $ids = array();
  9. 4 while (have_posts()) : the_post();
  10. 5 $ids[] = get_the_ID();
  11. 6 the_title();
  12. 7 the_content();
  13. 8 endwhile;
  14. 9 ?>
  15.  
  16. And, the second loop that will exclude the above posts and show the remaining ones :-
  17. view source
  18. print?
  19. 1 <?php
  20. 2 query_posts(array('post__not_in' => $ids));
  21. 3 while (have_posts()) : the_post();
  22. 4 the_title();
  23. 5 the_content();
  24. 6 endwhile;
  25. 7 ?>
  26.  
  27. This code basically pulls the post IDs that are not contained in the array $ids[].

Report this snippet  

You need to login to post a comment.