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

Create Custom Page with Custom Loop and Query

Created WordPress as CMS? I am guarantee you’ll need this tricks. Because WordPress Custom Page is Gregorius tools we can use to create custom design/layout and whatever we want. I will show you how to Create Custom Page with Custom … Read More

Display a sticky post only

WordPress is wonderful publishing platform, there are so many query that we can tweak it. On this chapter I will show you How to display a sticky post only in our loop. All need to do only written some code … Read More

Style Posts From Some Category Differently

Right now, every blogger has know WordPress as easiest and nicely support publishing system. To create more sophisticated and personal blog. Blogger chooses WordPress as they Blog Engine. Some featured that we can use to create more unique blog is … Read More

How To Create “Dynamics Statics” Pages

There are so many WordPress Tricks that we can apply. In our current section we will show you, how to create "Dynamics" "Statics" Pages in WordPress. That's I mean on this point is simple. We will generated semi automatics pages … Read More

How To Display Ads after first Post

Using WordPress and added extra code you can create wonderful Publishing System. Right now we will show you how to display Ads (Example Google AdSense) after first post of you WordPress. How To Display Ads after first Post is quietly … Read More

How to Get Most Commented Posts with Thumbnail

There are so many way to attract visitor on your WordPress blog, the one of them is show most popular post on your sidebar or footer, beside that fact in order to help your visitors finding your best content. Let's … Read More

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

Source: http://wptricks.net/how-to-setup-multiple-loops-in-wordpress/

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

Source: http://www.lovemikeg.com/2010/08/23/a-better-way-to-loop-in-wordpress-3/

Digging into WordPress Discount Code, Digging into WordPress - 30% OFF

Here are some of the awesome techniques that you will learn from Digging into WordPress: Setup multiple loops the right way; Setup page-specific menu styles read more

Source: http://wpsave.com/ebooks/digging-into-wordpress-discount-coupon-code/

Perishable Press Triple Loop for WordPress • Perishable Press

Here is the basic setup: // the second loop read more

Source: http://perishablepress.com/press/2006/11/22/perishable-press-triple-loop-for-wordpress/

Multiple Loops on your Main Page - Why and How

I found most of this information here http://codex.wordpress.org/The_Loop#Multiple_Loops and by I recently revealed a free video course named Setup for Success over read more

Source: http://wpmodder.com/multiple-loops-on-your-main-page-why-and-how-133.html


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>