The Loop
Understand the WordPress Loop — the PHP code that displays posts from the database on your pages.
The Loop
The Loop is the most fundamental concept in WordPress theme development. It's the PHP code that fetches posts from the database and displays them on the page.
What Is the Loop?
The Loop goes through each post that WordPress has prepared for the current page and outputs it. Whether you're on the homepage (showing recent posts), a category page, or a search results page — the Loop is what displays the content.
Basic Loop Syntax
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<article class="post-card">
<h2><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a></h2>
<p class="post-meta">
Posted on <?php the_date(); ?>
by <?php the_author(); ?>
</p>
<div class="post-excerpt">
<?php the_excerpt(); ?>
</div>
</article>
<?php endwhile; ?>
<?php else : ?>
<p>No posts found.</p>
<?php endif; ?>
How the Loop Works
have_posts()— checks if there are any posts to displaythe_post()— sets up the current post's data (title, content, date, etc.)- Inside the loop, you use template tags to output post data
- The
whileloop repeats for each post - The
elseblock handles the case where no posts exist
Common Template Tags
| Tag | What It Outputs |
|---|---|
the_title() | The post title |
the_content() | The full post content |
the_excerpt() | A short summary of the post |
the_permalink() | The URL to the full post |
the_date() | The post date |
the_author() | The post author's name |
the_category() | The post's categories |
the_post_thumbnail() | The featured image |
the_tags() | The post's tags |
Loop with Featured Image
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="post-card">
<?php if (has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('medium'); ?>
</a>
<?php endif; ?>
<div class="post-card-body">
<h3><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a></h3>
<p class="post-meta">
<?php echo get_the_date(); ?> |
<?php the_author(); ?>
</p>
<?php the_excerpt(); ?>
</div>
</article>
<?php endwhile; else : ?>
<p>No posts found.</p>
<?php endif; ?>
the_content() vs the_excerpt()
| Function | Use On | Behavior |
|---|---|---|
the_content() | Single post pages | Shows the full post content |
the_excerpt() | Archives, homepage | Shows first 55 words (or custom excerpt) |
The Template Hierarchy Connection
WordPress decides which template file to use and which posts to show based on the URL. The Loop always displays whatever posts WordPress has prepared for that template:
- Homepage → recent posts
- Category page → posts in that category
- Search results → matching posts
- Single post → just that one post
Key Takeaways
- The Loop is the core mechanism for displaying posts in WordPress
have_posts()checks for posts;the_post()sets up each post's data- Template tags like
the_title(),the_content(), andthe_permalink()output post data - Use
the_excerpt()on listing pages andthe_content()on single post pages - Always include an
elseblock for when no posts are found