Table of contents
Get the industry’s fastest WordPress hosting ◦ 99.999% uptime
◦ Comprehensive security
◦ 24/7 support

WordPress GuideDevelopment → Loop

What is the WordPress loop?

WordPress is written in PHP, a programming language frequently used on the web. PHP code generates the HTML pages sent to a browser by querying the database to gather content and combine it with templates. From this, a final output is generated and displayed to users.

This is why WordPress is so powerful. In addition to being a content management system, WordPress is a framework: it provides functions and APIs that developers can use to build a website. In essence, a WordPress theme is a bundle of code that takes advantage of this framework.

Get fast, reliable hosting for WordPress

Power your site with the industry’s fastest, most optimized WordPress hosting

Developing themes with the loop

One of the most important of the APIs provided by WordPress is The Loop. The Loop controls how content is displayed on a page. It’s used to display post listings, single posts, and pages.

It’s important to understand that a WordPress site doesn’t have only one loop. In fact, every major PHP file in a theme is a template file with a loop. This allows for WordPress developers to create unique page types that exploit their own Loop attributes.

The rest of a theme comprises CSS files and PHP files with functions that are imported into the template files.

An example of a WordPress loop

A basic WordPress loop looks like this:

There is quite a lot going on here, so we’ll explain in sections:

This line begins The Loop with an if statement that checks to see whether there are any posts to loop through. Then it begins a while loop, starting The Loop proper. The code between this line and the end of the while loop will be repeated for each post (which might only be one post, depending on which file this code appears in and the URL being accessed).

Adding content to the loop

In order to add content to a Loop, you need to mark it out using HTML with embedded PHP. You can add information like post titles, descriptions, content, dates, authors, and more. You can also include fixed repetitive content if you prefer a certain style.

This section contains HTML with an embedded PHP function for inserting the post title. In the code, this PHP function is called the_title, which is a template tag provided by WordPress.

Template tags are used to display information from the database. Here, the post title of the current post is inserted. The next few lines of code are similar, but the the_content template tag is used to insert the post’s content.

WordPress provides several hundred template tags, and themes are largely constructed from HTML, the output of template tags, and other PHP functions provided by WordPress or created by the developer. A typical WordPress template file is longer and more complex than this simple example, but works on the same principle.

To see The Loop in use, look inside your WordPress installation’s wp-content/themes directory. The following files are likely to include The Loop:

There are several other templates that might be present.

Ready to get started?

Get the fastest, most secure WordPress.org hosting on the market.

Additional resources

What is managed WordPress hosting? →

Get details and decide if managed WordPress hosting is right for you.

How to choose the right hosting provider in 2024 →

This guide offers a step-by-step strategy for selecting a reliable web hosting provider, whether you’re building a website for your business or looking for a reliable hosting partner to serve your clients.

A complete guide to WordPress shortcodes →

Shortcodes make life easier. Learn how to get started!

Trust us to help you choose the ideal hosting solution

Loading form…