Saturday, October 24, 2026The WordPress PlaygroundFacebookXPinterest
Glossary

The Loop

The Loop is the PHP code that WordPress themes use to display posts. It checks whether there are posts to show, then goes through them one by one and prints each post's title, content and other details.

Almost every page on a WordPress blog, from the home page to a category archive, is built with it. Let's understand it in simple words.

How Does the Loop Work in WordPress?

When someone opens a page, WordPress first works out which posts belong there. A category page needs posts from that category, while a single post page needs just one post. This is called the main query.

The Loop then takes those posts and handles them one at a time. Think of a teacher calling names from the attendance register: one name, one student, until the list ends.

What a Basic Loop Looks Like

Here is a simple Loop, similar to what you will find in classic theme files like index.php:

<?php
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        the_title( '<h2>', '</h2>' );
        the_content();
    }
}
?>

The have_posts() function checks if any posts are left, and the_post() sets up the next one. Inside the Loop, template tags like the_title(), the_content() and the_permalink() automatically know which post they belong to.

How Many Posts Does the Loop Show?

By default, your blog pages show 10 posts each. You can change this number in Settings → Reading with the Blog pages show at most option. Learn more in our lesson on WordPress reading settings.

When you need a second list, such as related posts in the sidebar, developers create a custom loop with WP_Query instead of changing the main one.

Note

Block themes don't need a hand-written PHP Loop. They use the Query Loop block, which does the same job visually in the Site Editor.

If you plan to build your own themes, our theme development overview is a good place to start.

That's all about the Loop. It is a small piece of code, but it powers most of your blog. Please leave your questions in the comment box below.