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
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.