Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP How to Build a WordPress Theme Extending WordPress Template Functionality Multiple Loops in One Template

Felix lim
Felix lim
6,049 Points

Why do we want to reset the loop

I am not sure about this sections, why do we reset these loops?

2 Answers

Andrew Shook
Andrew Shook
31,709 Points

Don't think of it in terms of resetting the loop, think of it in terms of resetting the call to the database that returns the values. For instance, say I want to list 5 similar articles on the side bar of a news website. But the side bar code is above the main article code. So I tell Wordpress, "hey find me 5 articles that have the same tag as this one." So Wordpress gets that from the database and I run it through a loop to print it out. Now I want to print the main article but Wordpress doesn't have that information stored in the $posts array. So now I need to tell WP to get that info, and since the main article is the default value return from the database for that page I would just "reset the loop".

Felix lim
Felix lim
6,049 Points

thanks, i understood now! :)