The Loop is used by WordPress to display each of your posts. Using The Loop, WordPress processes each of the posts to be displayed on the current page and formats them according to how they match specified criteria within The Loop tags. Any HTML or PHP code placed in the Loop will be repeated on each post. When WordPress documentation states “This tag must be within The Loop”, such as for specific Template Tag or plugins, the tag will be repeated for each post.
In plain English, this means that the magic of WordPress is the structured way in wich it iterates through posts, template tags, and files. Have you ever seen a really minimalist WordPress site and wondered how they’re excluding all the extraneous visual noise? At the heart of their customized look is a theme that’s only selecting a minimum of content to display for every post. You don’t even have to display a headline on each post if you don’t want to—it’s up to you.
The simplest example of a functional
index.php page in WordPress would be as follows:
<?php get_header(); if (have_posts()) : while (have_posts()) : the_post(); the_content(); endwhile; endif; get_sidebar(); get_footer(); ?>
It’s surprisingly easy to understand what’s going on in this snippet:
- Grab our header file
- While there are posts to display, show each of them
- Grab our sidebar file
- Grab our footer file
What’s important to note here is that there is no one file you’d edit to make thorough changes to your WordPress site’s functionality. The theme files are stitched together on the fly, which gives us the ability to structure our code in a very manageable way.