The WordPress Loop is the code that WordPress uses to display content on your website.
Take your index.php template, for example. In a basic blogging theme, this template is generally used to display several posts or post excerpts on your home page. The index.php template uses the WordPress Loop in order to do this.
A basic understanding of the WordPress loop is necessary if you want to modify your WordPress design. Even if you do not have experience with PHP or HTML, you should be able to understand how the WordPress Loop is constructed after reading this tutorial.
Understanding the WordPress Loop
The best way to learn about the WordPress Loop is to look at a basic example of how it is used in a WordPress theme. Therefore, let us look at some simple code initially and then I can break down each line to give you a better understanding of what each line does.
Below is an example of a simple WordPress Loop. The code in your own WordPress theme for the loop may be much longer, however it follows the same structure as this.
&lt;?php if ( have_posts() ) : while ( have_posts() ) : the_post(); // // Post Content here // endwhile; // end while endif; // end if ?&gt;
If you have some experience using PHP, the above code will be self-explanatory; however, let us take a closer look at each line for the benefit of those who do not.
The first thing we do is advise your server that we are going to use PHP. We open PHP statement by using <?php.
In the next line, we have a basic “if statement” using the have_posts function. The have_posts WordPress function is a boolean function; which means that the result is either true of false.
Therefore, the following line of code effectively says “If there are some posts, display this line of code, if not, do nothing”.
if ( have_posts() ) :
In the next line, we use a while loop. A while loop will execute a piece of code as long as something is true. In this case, we are saying that while there are posts to be displayed, execute the following line of code.
Therefore, if you had configured your WordPress reading settings to display five posts on the home page, the while function would execute the statements contained within the while loop five times and then stop.
while ( have_posts() ) :
We then call the data from the next post by using the WordPress function the_post. This sets up the post and allows us to retrieve any part of the post including the content, the publication date, the author, the category it was published in, and much more later on.
Once we have called up our post, we can display anything we want from it. There are over one hundred template tags available that can only be used within the WordPress Loop.
// // Post Content here //
After we have confirmed the information that we want displayed with every post, we close the while loop.
endwhile; // end while
We then close the if statement.
endif; // end if
Finally, we end by closing PHP.
As you can see, when you break down the WordPress Loop, it is very easy to understand.
Examples of the WordPress Loop
In the WordPress codex, WordPress shares the code for the world’s smallest index.php template. As you can see from this code (shown below), all the index.php template contains is a call to the header, the WordPress Loop, a call to a sidebar, and a call to a footer. It perfectly illustrates how simple WordPress can be.
&lt;?php get_header(); if (have_posts()) : while (have_posts()) : the_post(); the_content(); endwhile; endif; get_sidebar(); get_footer(); ?&gt;
The code for the WordPress Loop looks a little different in most WordPress themes because the information that is displayed is a little different. Examining the WordPress Loop in different designs is therefore a great way of seeing exactly how the loop works in practice.
Below is the code that is used for the WordPress Loop in the index.php template of the current WordPress default theme TwentyFourteen.
As you can see, TwentyFourteen calls a different template for the content area depending on what type of post format is called e.g. a standard post will display different information from video posts and quote posts.
Navigation links are displayed after the last post and a message is displayed to visitors if no posts have been found (they do this by calling the template content-none.php).
&lt;?php if ( have_posts() ) : // Start the Loop. while ( have_posts() ) : the_post(); /* * Include the post format-specific template for the content. If you want to * use this in a <a class="wpil_keyword_link" href="https://www.elegantthemes.com/blog/resources/wordpress-child-theme-tutorial" title="child theme" data-wpil-keyword-link="linked">child theme</a>, then include a file called called content-___.php * (where ___ is the post format) and that will be used instead. */ get_template_part( 'content', get_post_format() ); endwhile; // Previous/next post navigation. twentyfourteen_paging_nav(); else : // If no content, include the &quot;No posts found&quot; template. get_template_part( 'content', 'none' ); endif; ?&gt;
The WordPress Loop is easier to follow in TwentyFourteen and TwentyThirteen because the code for content and meta information is placed in a separate template. It is also a more practical way of structuring code for themes that support post formats.
If we take a look back to the popular theme Twenty Twelve, we can see that the WordPress Loop still calls other template files; however a lot of code remains within the WordPress Loop in the index.php template.
&lt;?php if ( have_posts() ) : ?&gt; &lt;?php /* Start the Loop */ ?&gt; &lt;?php while ( have_posts() ) : the_post(); ?&gt; &lt;?php get_template_part( 'content', get_post_format() ); ?&gt; &lt;?php endwhile; ?&gt; &lt;?php twentytwelve_content_nav( 'nav-below' ); ?&gt; &lt;?php else : ?&gt; &lt;article id=&quot;post-0&quot; class=&quot;post no-results not-found&quot;&gt; &lt;?php if ( current_user_can( 'edit_posts' ) ) : // Show a different message to a logged-in user who can add posts. ?&gt; &lt;header class=&quot;entry-header&quot;&gt; &lt;h1 class=&quot;entry-title&quot;&gt;&lt;?php _e( 'No posts to display', 'twentytwelve' ); ?&gt;&lt;/h1&gt; &lt;/header&gt; &lt;div class=&quot;entry-content&quot;&gt; &lt;p&gt;&lt;?php printf( __( 'Ready to publish your first post? &lt;a href=&quot;%s&quot;&gt;Get started here&lt;/a&gt;.', 'twentytwelve' ), admin_url( 'post-new.php' ) ); ?&gt;&lt;/p&gt; &lt;/div&gt;&lt;!-- .entry-content --&gt; &lt;?php else : // Show the default message to everyone else. ?&gt; &lt;header class=&quot;entry-header&quot;&gt; &lt;h1 class=&quot;entry-title&quot;&gt;&lt;?php _e( 'Nothing Found', 'twentytwelve' ); ?&gt;&lt;/h1&gt; &lt;/header&gt; &lt;div class=&quot;entry-content&quot;&gt; &lt;p&gt;&lt;?php _e( 'Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve' ); ?&gt;&lt;/p&gt; &lt;?php get_search_form(); ?&gt; &lt;/div&gt;&lt;!-- .entry-content --&gt; &lt;?php endif; // end current_user_can() check ?&gt; &lt;/article&gt;&lt;!-- #post-0 --&gt; &lt;?php endif; // end have_posts() check ?&gt;
It is best to remember the “Don’t Repeat Yourself” coding principle. By reducing repetition, you can create cleaner, leaner themes that are easier to modify.
The WordPress Loop is used by WordPress to publish content. In its simplest form, the WordPress Loop simply checks if there are posts or pages to be displayed and then displays them.
By using multiple loops and modifying loops using the WP_Query class, theme developers can design complex website themes. The code for this is sometimes complicated, yet the general principle of how developers create complex themes is based on the basic concept of the WordPress Loop.
In this article, I have explained what the WordPress Loop is for and showed how it is used in themes to display posts and pages.
For more information on the WordPress Loop, please refer to the following pages in the WordPress codex:
I hope you have enjoyed this tutorial. Be sure to subscribe to Elegant Themes if you would like updates of our latest articles.
Article thumbnail image by mejnak / shutterstock.com