Even if you aren’t aware of it, if you’ve spent anytime in WordPress, you are familiar with the WordPress loop. It is the method WordPress uses to display posts on a page. The Loop allows for not only displaying the posts but for you to be able to modify it, if needed.
When we talk about The Loop (yes it is capitalized), we typically mean the main loop on a page. Let’s say that your front page displays your 5 most recent posts. Those posts are displayed using The Loop. The Loop should be placed in your index.php file and in any other templates used to display post information.
What does it look like?
The Loop is a block of PHP code that looks like this:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<!-- do stuff ... -->
<?php endwhile; ?>
<?php endif; ?>
In English the above would read: If we are going to be displaying posts, then get them, one at a time. For each post, display it according to <!– do stuff…–>.When you hit the last post, stop. Pretty straight forward right?
More magic occurs in the <!– do stuff…–> section, where you can set how the posts are to be displayed. You can add the author, the date, the categories etc., using Template Tags. We will cover Template tags in another article.
The Loop is where WordPress’s power comes from. It allows you to display your posts where you want and how you want. Look at the code from your favourite theme and you’ll find The Loop in the index.php file for that theme. While the code will differ slightly depending on how the theme author chooses to display the content, The Loop will be the base from which it all comes from.