ZeroToWP

WordPress Loop

Quick Definition

The Loop is the PHP code WordPress uses to display posts. It cycles through each post that matches the current page query and outputs the title, content, date, and other details using template tags.

WordPress Theme Handbook documentation explaining The Loop

What Is the WordPress Loop?

The WordPress Loop (usually just called "The Loop") is the PHP code that cycles through your posts and displays them on the page. Every time WordPress renders a blog archive, a category page, a search result, or a single post — The Loop is what makes it happen.

The basic Loop structure looks like this:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?>
<?php endwhile; endif; ?>

Here is what each part does:

  • have_posts() — Checks if there are posts available to display. Returns true or false.
  • the_post() — Loads the next post and prepares its data for use by template tags.
  • the_title() — Outputs the current post's title.
  • the_content() — Outputs the current post's full content.

The Loop runs once for each post that matches the current page's query. On a blog archive showing 10 posts per page, The Loop runs 10 times. On a single post page, it runs once. The query that determines which posts to show is handled by WordPress's WP_Query class — The Loop just displays whatever the query returns.

Inside The Loop, you can use over 100 template tags to display post data:

  • the_title() — Post title
  • the_content() — Full post content
  • the_excerpt() — Post excerpt
  • the_permalink() — Post URL
  • the_post_thumbnail()Featured image
  • the_category() — Post categories
  • the_author() — Author name
  • the_date() — Publication date

These template tags only work inside The Loop. Using them outside The Loop produces no output or errors.

The WordPress Loop in Practice

In classic themes, The Loop is the heart of every template file: index.php, single.php, archive.php, search.php, and page.php all contain a Loop. Theme developers customize what displays inside The Loop and how it is styled — but the Loop structure itself stays the same.

In block themes, The Loop is replaced by the Query Loop block — a visual, no-code equivalent. Instead of writing while ( have_posts() ), you drag a Query Loop block into the Site Editor and configure it visually. The result is the same — posts are displayed — but the implementation is blocks instead of PHP.

For custom queries (showing posts from a specific category, displaying related posts, or creating a custom post grid), developers use WP_Query with a custom Loop:

$custom = new WP_Query( ['category_name' => 'tutorials', 'posts_per_page' => 5] );
while ( $custom->have_posts() ) : $custom->the_post();
    // Display post
endwhile;
wp_reset_postdata();

Why It Matters

The Loop is how WordPress turns database content into web pages. Understanding it unlocks theme development, custom templates, and the ability to display any content anywhere on your site. Even if you never write PHP, knowing that The Loop exists helps you understand why block themes replaced it with the Query Loop block — and appreciate what the block editor is doing for you behind the scenes.

Sources

Related Terms

Related Articles