Blog

Using multiple WordPress loops

By:
on:

Coding more complex WordPress sites means you'll probably need more than one loop on certain pages. For example, a homepage with different sections and categories, or one page designs that are really trending this year.

There are a couple ways to loop in WordPress, and this is how I usually do it.

The example below features two loops. As you can see I use the rewind_posts() function on line 8. This example can be used in any WordPress template: index.php, single.php, ...

// First loop (get the last 4 posts in the "blog" category)





    





// loop reset





// Second loop (get all posts)



    

Why use rewind_posts()

This piece of code doesn’t use any hacks, rewind_posts() is a standard WordPress function.

You use it to “clear” a loop that has been used previously (like the first loop in our example above), allowing you to use a second loop that isn’t affected by the first loop’s results.

Now do your thing, good luck!

KOEN IS HAPPY TO INTRODUCE APPSALOON
APPSALOON BV
Bampslaan 21 box 3.01
3500 Hasselt
Belgium
VAT: BE08 4029 5063
2021 © AppSaloon All Rights Reserved
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram