What is the WordPress Loop?

The Loop is the code which WordPress uses to show posts in a template. It checks if there are posts, goes through them one by one, and prints the post data with template tags.

<?php if ( have_posts() ) : ?>
	<?php while ( have_posts() ) : the_post(); ?>
		<h2><?php the_title(); ?></h2>
		<?php the_excerpt(); ?>
	<?php endwhile; ?>
<?php else : ?>
	<p>No posts found.</p>
<?php endif; ?>
  • have_posts(): are there posts left?
  • the_post(): move to the next post and set up its data.
  • the_title(), the_content(): print the details of the current post.

See The WordPress Loop for a tested example and WP_Query for custom loops.


Learn more about the similar topics:
Tutorials
WordPress Themes
WordPress Top Theme List
WordPress Theme Files and Functions
WordPress Template Hierarchy
WordPress Enqueue Scripts and Styles
WordPress Loop
WordPress WP_Query Class
Exercises & Assignments
Build and activate a minimal classic theme
Show the three latest posts with WP_Query
Load a stylesheet with wp_enqueue_style and a version
Show a message only on the front page
Create a child theme which changes the footer
Interview Questions & Answers
What is a WordPress theme?
What are commonly used WordPress theme functions?
Which function is used to get the home URL in WordPress?
Which are the necessary files for a WordPress theme?
What is the use of the wp_head() function in WordPress?
What is the WordPress Loop?
What is the difference between is_home() and is_front_page()?