One of the most important things to learn when it comes to WordPress is “the loop”. It’s the heart of the system. Let’s take a look at the infamous loop!
1 2 3 4 5 6 7 8 9 10 11 12 13 | <!--?php if (have_posts()) : ?--> <!--?php while (have_posts()) : the_post(); ?--> // Loop information to display posts goes here... <!--?php endwhile; ?--> <!--?php else: ?--> // If no posts found, what should I display? <!--?php endif; ?--> |
Above is the basic loop structure. Let’s break it down in plain English.
- Are there any posts?
- If yes, continue to display them until there aren’t any left.
- If no, display something letting the visitor know there are no posts available.
Where do I find the loop?
The loop can be found on theme template files like archive.php, index.php, and search.php.
What does the loop look like with actual template tags?
Now that we have a general understanding of how the loop works, let’s show an example of how to display information within the loop.
1 2 3 | <!--?php if (have_posts()) : ?--> <!--?php while (have_posts()) : the_post(); ?--> |
Nothing Found
Sorry, there are no posts.
There may be some things you might not recognize above if you’re not familiar with WordPress Template Tags. You should definitely read up on them when trying to understand how WordPress functions.
There are so many ways the loop can be used. It’s really up to you and how you want to style the content. Have fun with it, the possibilities are endless!