query_posts
One of the interesting things of WP 1.5 is the ability to rerun The_loop again in the same template. The main way to do this is to execute a rewind_posts(); after the_loop. This will reset the post counter so that you can make another loop round.
The slightly more interesting way to use multiple loops is to make use of query_posts(). What this basically does is reinitialises the query object. The clever thing about this function is that you can pass it parameters to make it a totally different query. We can, for example, make the second loop fetch back only posts in the 'shorts' category.
The following snippet illustrates one of the ways we can do this. The general format of index.php is the following
if (have_posts()) :while (have_posts()) : the_post();the_title(); //do your stuffendwhile;endif;
What we can do after going round the loop once, is execute query_posts. We'll pass in a parameter to get it to fetch only posts from the site category. This will rebuild the posts collection and *implicitly* resets the post counter.
Hence, the page will be:
if (have_posts()) :while (have_posts()) : the_post();the_title();endwhile;endif;//now run a new queryquery_posts('category_name=shorts');//as $wp_query is reinitialised, let's run the_loopif (have_posts()) :while (have_posts()) : the_post();the_title();endwhile;endif;
If you want more information on the arguments which you can pass to query posts, see part 2: query_post_redux
-30-
[…] sides, if..else style
I’ve implemented asides using the query_posts function as described here by Phu Ly.
Posted by Brendan 2 minutes ago in Asides
[…]
Thanks for this article!
Think abaout rewriting the “WP-Loop” atricle in the WP-Doc Wiki, because your one is quite mor useful.
Great stuff. I am currently redesigning my site, and this will come in really handy.
Thanks!
porfavor denme un mono ppara adoptar por que yo quiero adoptar a una mascotita que mas da gracias de todas formas si no pueden
[…] Phu uses query_posts to get WordPress to create multiple loops. Very useful. […]
great…make the redesigning more handy