SponsoredHosinger Special Offer

WP WordPress Theme Development

Build classic and block WordPress themes from scratch: the template hierarchy, The Loop, the_title() and the_content(), template parts, menus, the Customizer, child themes and theme.json.

The Loop: have_posts() and the_post()

WordPress Theme Development Lesson 6 of 30 ~3 min read

Overview

The Loop is the block of PHP that walks through the posts WordPress already fetched for the current page and prints each one — every template that shows content is built around it.

Remember from lesson 1 that WordPress runs the database query before your template loads. Your template does not fetch posts. It only loops over what is already there.

The standard Loop

<?php if ( have_posts() ) : ?>

    <?php while ( have_posts() ) : the_post(); ?>

        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <?php the_excerpt(); ?>
        </article>

    <?php endwhile; ?>

<?php else : ?>

    <p><?php esc_html_e( 'Sorry, no posts matched your criteria.', 'gdc-starter' ); ?></p>

<?php endif; ?>

What each part does

CodeWhat it does
have_posts()Returns true if there are posts left to show in the main query.
while ( have_posts() )Keeps looping while posts remain.
the_post()Moves to the next post and sets up the global $post so template tags know which post they are describing. Forget it and you get an infinite loop.
the_ID(), the_title() …Template tags. Each prints one piece of the current post.
elseRuns when the query found nothing — a search with no results, an empty category.
Watch out: Never omit the_post(). Without it, have_posts() stays true forever and the page loops until PHP runs out of memory.

Why the alternative syntax?

You will notice while ( ... ) : and endwhile; instead of curly braces. Both work identically. The colon form is preferred in templates because a lone } among dozens of lines of HTML is impossible to match by eye, while endwhile; and endif; say exactly what they close.

Where the posts come from

The Loop reads from the main query, which WordPress built from the URL:

URLThe main query contains
/The latest posts (or the front page)
/category/news/Posts in the News category
/2026/10/hello-world/One post — the Loop runs once
/?s=themePosts matching the search "theme"

The same Loop code works on every one of them. That is why a single index.php can power an entire site.

Counting and styling items

The global $wp_query knows where the Loop is. Use it to style the first post differently or add a divider between items:

<?php while ( have_posts() ) : the_post(); ?>

    <?php
    global $wp_query;
    $is_first = ( 0 === $wp_query->current_post );
    ?>

    <article <?php post_class( $is_first ? 'post--featured' : '' ); ?>>
        <?php the_title( '<h2 class="entry-title">', '</h2>' ); ?>
    </article>

<?php endwhile; ?>

post_class() accepts extra class names and merges them with the ones WordPress generates — such as post, type-post, status-publish, category-news and has-post-thumbnail — which gives you hooks for CSS without any extra PHP.

Common mistakes

  • Calling query_posts() inside a template to change the posts. It replaces the main query, breaks pagination and is discouraged. Use pre_get_posts (lesson 21) or a separate WP_Query.
  • Using $post->post_title directly. It skips the filters plugins rely on. Use the_title() or get_the_title().
  • Running a second Loop over a custom query and forgetting wp_reset_postdata() — covered in lesson 21.

Practice

  • Put the standard Loop into index.php and confirm your imported test posts appear.
  • Search for a word that does not exist and confirm your else message shows.
  • Add a post--featured class to the first post only and style it in style.css.
Create a free account to save which lessons you've finished. Save my progress