Chapter 3. THE LOOP

Now that you have the basic WordPress installation as well as the theme concept under control, it is time to look at what really makes the system run. This chapter will teach you about the loop, which basically is a PHP query that talks to WordPress and makes it output the things requested. The chapter will start with some basic usage, and then branch out to multiple loops and some nice little trickery used within to achieve various effects.

You need to understand the loop to really create cool WordPress sites, and while you won't need to know it by heart, you should grasp what it does. That way, you can research the many functions and features available when you run into a solution that requires custom content output.

Understanding the WordPress Loop

The loop is the heart of WordPress, and it resides in your theme's template files. While you can in fact have a theme without the loop, it would make the fluidness of the content handling, like displaying the latest posts and browsing backwards, quite difficult to pull off. Some template files, for example 404 error pages, don't have the loop at all, but most do. If you want to look for it in a theme template file, open index.php; this is the fallback file as you'll recall, hence it needs the loop to display content listings and single posts as well as pages.

Some template tags only work within the loop, so you need to be able to identify it. This is easy, as you will see in the next section.

The Basic Loop

If you want ...

Get Smashing WordPress: Beyond The Blog now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.