WordPress Loop
The Loop is the PHP code that WordPress uses to show posts. For each page, WordPress first runs a database query and keeps the result, then your template loops over that result and prints every post. The Loop is used in almost every theme file.
The Loop structure
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<!-- code for one post -->
<?php endwhile; ?>
<?php else : ?>
<!-- message when there is no post -->
<?php endif; ?>
have_posts()checks if there is any post left to show.the_post()takes the next post and sets up the global post data, so the template tags work.- The
elsepart shows a message, like on a search with no result.
The Loop in action
We create three posts, and write a loop which shows the title, date, category and an excerpt:
<?php get_header(); ?>
<main>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="meta"><?php the_date( 'j F Y' ); ?> | <?php the_category( ', ' ); ?></p>
<?php the_excerpt(); ?>
</article>
<?php endwhile; ?>
<?php else : ?>
<p>Sorry, no posts found.</p>
<?php endif; ?>
</main>
<?php get_footer(); ?>
$ curl -s http://example.com/ | sed -n '/<main>/,/<\/main>/p' | head -30
<main>
<article id="post-7">
<h2><a href="http://example.com/hello-from-the-loop/">Hello from the loop</a></h2>
<p class="meta">4 February 2026 | <a href="http://example.com/category/uncategorized/" rel="category tag">Uncategorized</a></p>
<p>This is the newest post of the demo.</p>
</article>
<article id="post-5">
<h2><a href="http://example.com/learn-php-basics/">Learn PHP basics</a></h2>
<p class="meta">3 February 2026 | <a href="http://example.com/category/uncategorized/" rel="category tag">Uncategorized</a></p>
<p>Start with variables and functions. They are the base of PHP.</p>
</article>
<article id="post-6">
<h2><a href="http://example.com/wordpress-themes/">WordPress themes</a></h2>
<p class="meta">2 February 2026 | <a href="http://example.com/category/uncategorized/" rel="category tag">Uncategorized</a></p>
<p>A theme controls the look of your site.</p>
</article>
<article id="post-1">
<h2><a href="http://example.com/hello-world/">Hello world!</a></h2>
<p class="meta">5 January 2026 | <a href="http://example.com/category/uncategorized/" rel="category tag">Uncategorized</a></p>
<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>
</article>
</main>
The posts come newest first, in the order of the date.
Template tags inside the Loop
| Tag | What it prints |
|---|---|
| the_title() | Title of the post |
| the_content() | Full content |
| the_excerpt() | Short summary |
| the_permalink() | URL of the post |
| the_ID() | ID of the post |
| the_author() | Name of the author |
| the_time(), the_date() | Date and time |
| the_category(), the_tags() | Categories and tags (as links) |
| the_post_thumbnail() | Featured image |
Tags that start with the_ print the value, while the ones that start with get_the_ return it, so you can use it in your own code, for example $title = get_the_title();.
How the Loop works inside
Before your template runs, WordPress reads the URL, builds a main query and saves it in the global $wp_query object. The posts array is inside it. Each call of the_post() moves to the next item and fills the global $post. The same works for your own query objects. Here we ask for two posts only, but found_posts still counts all the posts which match:
$q = new WP_Query( array( 'posts_per_page' => 2 ) );
echo 'Posts in the database: ' . $q->found_posts . "\n";
while ( $q->have_posts() ) {
$q->the_post();
echo get_the_ID() . ' - ' . get_the_title() . "\n";
}
wp_reset_postdata();
Posts in the database: 4
7 – Hello from the loop
5 – Learn PHP basics
Pagination
The number of posts per page is set in Settings > Reading. Add links to the next and older pages with the function below (it prints nothing when there is only one page):
<?php the_posts_pagination( array(
'mid_size' => 2,
'prev_text' => 'Previous',
'next_text' => 'Next',
) ); ?>