How To Setup Multiple Loops in WordPress


Sponsored Links




WordPress built in with many query method, our example below about query is the best way to create Multiple Loops.  I will show you how to  do multiple loops in wordpress. let us assume we want to have two lists of posts. One which would list the most recent posts (the standard 10 posts most recently added), and another which would contain only one post from the category ‘featured’. Posts in the ‘featured’ category should be shown first, followed by the second listing of posts (the standard).

Step 1. Get only one post from the ‘featured’ category.

<?php $my_query = new WP_Query('category_name=featured&posts_per_page=1');
  while ($my_query->have_posts()) : $my_query->the_post();
  $do_not_duplicate = $post->ID; ?>
    <!-- Your First Looping Code will be here -->
  <?php endwhile; ?>

This query is set $my_query equal to the result of querying all posts where the category is named featured and by the way, get me one post only. Also, set the variable $do_not_duplicate equal to the ID number of the single post returned. Recall that the Do stuff line represents all the formatting options associated for the post retrieved.

Note that we will need the value of $do_not_duplicate in the next step to ensure that the same post doesn’t appear in both lists.

Step 2. The second loop, get the X latest posts (except one).

This code below will be do some query except the one post that has been displayed on the first query..

<?php if (have_posts()) : while (have_posts()) : the_post();
  if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
   <!-- Your Second Looping Code will be here -->
  <?php endwhile; endif; ?>

That code will be displayed latest post except 1 post that has been displayed on the first loop. So the completed code will be like this

<?php $my_query = new WP_Query('category_name=featured&posts_per_page=1');
  while ($my_query->have_posts()) : $my_query->the_post();
  $do_not_duplicate = $post->ID;?>
    <!-- Looping Code -->
  <?php endwhile; ?>
    <!-- Do other Loop -->
  <?php if (have_posts()) : while (have_posts()) : the_post();
  if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
   <!-- Looping Code -->
  <?php endwhile; endif; ?

I hope this tutorial how to setup multiple loops in WordPress blog is clearly for you and have a nice day, with this tutorial, we can create unique page templates. We can added extra query in our Page Template.

Subscribe to Full RSS Feed

RSS FeedIf you thing this article useful, please consider subscribing to our RSS Feed or e-mail updates and stay updated with us. You can also follow @WPTricksNet on twitter for latest updates.

About WP Tricks

We are WordPress Family, we collected and writed useful tips and tricks for you WordPress. We share it for you...

Related Tips and Trick from Local Data and on The Net

How To Setup Multiple Loops in WordPress / WordPress Tricks & Tips

WordPress built in with many query method, our example below about query is the best way to create Multiple Loops. I will show you how to do multiple loops in Read More

WordPress › Support » new page template with multiple loops

[resolved] new page template with multiple loops, now blog foreach($latest_news as $post) : setup template file, you might want to use the WordPress Read More

Perishable Press Triple Loop for WordPress : Perishable Press

Here is the basic setup: // the second loop Read More

WordPress › Support » Multiple loops, Pagination not working

my main index page like a magazine style, with multiple loops. offset=1"); foreach($myposts as $post) : setup This topic is not resolved; WordPress version: 3.0.1 Read More

A Better Way to Loop in WordPress 3 | Love Mike G

problems with setup_postdata is that it clobbers the original global post data. WordPress how to juggle multiple loops on a single page. To make things easier, WordPress Read More


6 Responses to How To Setup Multiple Loops in WordPress

  1. Pingback: How To Create "Dynamics Statics" Pages on WordPress # WordPress Tricks & Tips

  2. Pingback: How To Create “Dynamics Statics” Pages | Silver Blog

  3. Pingback: How To Create “Dynamics Statics” Pages | Unlock IMEI Cellphones

  4. Marcello says:

    Hi,
    thanks for this wonderful post! As a designer (with a very small knowledge of php) I found it very clear and complete.

    I have a problem: I’ve used your suggestion in the homepage of http://www.recruitingclub.at but there’s a small problem. When you go to the older articles (“Ältere Artikel” at the bottom of the page), you still get the 2 latest articles. Do you know how to do to show a personalized loop only in the first page of a pagination?

    Thanks a lot!

  5. Jeff Foxx says:

    Thanks! This is what I have been looking for. Don’t forget the closing “>” on the completed code view.

  6. sudeep says:

    I am planing to customize my blog, This article would be very helpful.
    Thank you.

    Sudeep

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>