The Montana Webmaster Team
Helping You With All Your Web Development Needs

That Mysterious WordPress ‘Loop’

Introduction

If you have ever explored WordPress outside of the dashboard, you may have come across this “loop” thing. What is that? A “loop” is a programming word designed to make you sound very geeky. In programming, a “loop” does something until it’s supposed to stop. So, for example, when you wash dishes, you are in a loop until there are no more dishes to wash – unless you are using a dishwasher, which is an entirely different module altogether. So, you wash a dish, see if there is another dish, if there are no more dishes, you stop, if there is another dish, wash the dish and so on.

The geekiest of descriptions about the “loop.” This author takes no responsibility for emotional trauma incurred in reading the codex version.

So, What is the WordPress “Loop”?

The loop is code that does the following task:

  1. Get what type of page are you are linking to – a category page, the home page, a post page?
  2. Go to the database and see if there is anything that matches what type of page you are linking to.
  3. If there isn’t anything in the database that matches, tell the user to go away and stop bothering you. (OK, maybe give the user a nicer message.)
  4. If there is something in the database that matches, grab it. This is called a “dataset.”
  5. Now, look at the dataset, get the first item and put it on the page.
  6. Is there a second item?
  7. No? Go take a coffee break; your work is done. (20 years ago it was a cigarette break, but even WordPress is now health conscious.)
  8. Yes? Put that item on the page.
  9. Is there a third item?
  10. No? Go take a coffee break; your work is done.
  11. Yes? Put that item on the page.
  12. Does this sound like a loop? :-) Now you got it!

By they way, when you went to this post, the loop only looped one time – because, well, this post is only one item! However, if you went to my page, and scrolled down the page, you would see that the loop picked up many posts! Now, how did we get the full post to show on the first one, while the others show the excerpt? That, my friends, is an ancient Chinese secret!

Where is ‘the loop’?

There are two areas called “the loop.” The first place you will encounter the loop is in the WordPress files that create the main body of your pages. These are pages like, index.php, page.php, single.php, etc. These pages aren’t the loop, they just use the loop. Even the WordPress Codex page tells you more about how to use the loop than what actually makes it work and where the machinery is hidden. So, the “real” loop is really somewhere else.

The loop is php code, which means that it is in one of those .php files. There are an awful lot of those; so, the first thing is to organize them into sets for your brain to get. First, there are the php files that aren’t in any WordPress folders. Those are “the core” – the most highly sacred ground in WordPress. There are also php files in the wp-includes folder, which are part of “the core.” These are files that you will want to explore when you are on your way to sending major improvements to WordPress as one of their most high and holy WordPress gurus. Until then, the loop and everything you need is in your own child theme.

Where “the loop” is in your theme depends on 1) how old your WordPress is, 2) whether you are using a child theme. In WordPress version 2.9.2, the code that ran the loop was in a file called query.php in the wp-includes folder.  The code for the content areas, such as index.php, page.php, single.php called this code with if (have_posts()) : while (have_posts()) : the_post(); and other code that indicated whether the loop was for a single page/post or a list of pages/posts for a category or search. With WordPress 3.+ things changed a bit.

About The Author


Nora McDougall-Collins started her online career in the newspaper industry, working with the staff of small community newspapers to put their content online. Since then she has been training small business owners and staff about marketing their businesses online and how to create and maintain websites.

Leave a Reply