WordPress Loop

WordPress Loop is a PHP code to fetch and display content in WordPress themes.

WordPress Loop, or simply The Loop is the most important part of the WordPress themes. You will find them in different template files such as index.php, page.php, single.php, archive.php, and category.php.

Using Loop, WordPress processes each post (or page) from the database based on parameters. Loop controls how your content will be displayed on home page, category page or post detail page.


The Loop – Structure:

<?php 
if ( have_posts() ) : 
    while ( have_posts() ) : the_post(); 
        // Display post content using template tags
    endwhile; 
endif; 
?>
  • The have_posts() function checks whether WordPress has any posts to loop over.
  • If a post exists, a while loop continues to fetch each post until posts exist.
  • You can use various parameters before have_posts() to filter content based on pages, posts, categories, tags etc.

The Loop – Example:

Inside loop, you can customize post data using PHP and HTML. You can choose what information you want to display for each post.

Let us suppose you want to display customized information on a single (detail) post page. You want to display the post title inside <h2> tag, followed by the feature image (attached to post), and then post content in <div> tag.

In the WordPress theme hierarchy, single.php and singular.php file can be used to display single post data on a webpage. Therefore, we need to use relevant Template Tags inside the Loop to display those information in any of these files. Here is The Loop example.

<?php 
if ( have_posts() ) : 
    while ( have_posts() ) : the_post();  ?>
        <h2> 
        	<?php the_title(); ?>
        </h2>
        <?php the_post_thumbnail(); ?>
		<div class="entry">
			<?php the_content(); ?>
		</div>
<?php
    endwhile;
endif; 
?>

You can try the above code by replacing the existing loop inside ‘single.php’ or other WordPress ‘twentytwentyone’ or other default theme.


How Loop works internally:

The have_posts() function internally call WP_Query method stored in wp_query global variable i.e. $wp_query->have_posts(). The $wp_query stores query parameters for current page to run a database query. Then, the_post() calls get_posts() internally to perform database queries enable us to loop (similar to PHP foreach loop) so that we can iterate data received from database.

However, most developers do not need to understand internal complex functions. You just need to learn about the WordPress Loop related functions and template tags to work with WordPress data.

You can the override the main query using query_posts() before Loop. For example, if you want to display 5 posts from category id 3, then use query_posts() as given below:

<?php 
query_posts( 'cat=3&posts_per_page=5' );
if ( have_posts() ) : 
    while ( have_posts() ) : the_post(); 
        // Display post content using template tags
    endwhile; 
endif; 
?>

Actually, you should never use query_posts() because it modify main query. This can create bugs in some situations, for example when you are using multiple loops and query_posts() with different parameters. It is recommended to use get_posts() and WP_Query to create custom queries as they doesn’t modify global variables.

Important Links:


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.