How to Use the Loop in the Twenty Fifteen WordPress Main Index Template - dummies

How to Use the Loop in the Twenty Fifteen WordPress Main Index Template

By Lisa Sabin-Wilson

The Loop in the case of the Main Index template is a function that WordPress uses to display content on your site such as blog posts and page content. The Loop has a starting point and an ending point; anything placed in between is used to display each post, including any HTML, PHP, or CSS tags and codes.

Quite a few variations of the WordPress Loop are available, but here you will find the most common ones. When you understand the common use, you can begin to push the envelope a bit further and use variations to suit your needs. This common use displays your posts in chronological order, starting with your most recent post and followed by less recent posts, ordered by date.

The basic and most common use of The Loop in WordPress is to pull blog posts from the database and display them on your website. In the default Twenty Fifteen theme, you find the loop in the Main Index template (index.php) and it contains these four lines of code:

<?php if ( have_posts() ) : ?>
// Start the Loop.
while ( have_posts() ) : the_post();

At the bottom of the template, The Loop ends with these lines of code:


Here’s a look at what the WordPress Codex calls “The World’s Simplest Index”:

if (have_posts()) :
while (have_posts()) :

First, the template starts by opening the php tag. Next, it includes the header, meaning that it retrieves anything contained in the header.php file and displays it. Now the good stuff starts happening. The Loop begins with the while (have_posts()) : bit. Anything between the while and the endwhile repeats for each post that displays. The number of posts displayed is determined in the settings section of the WordPress Dashboard.

Misplacement of the while or endwhile statements causes The Loop to break. If you’re having trouble with The Loop in an existing template, check your version against the original and see whether the while statements are misplaced.

In your travels as a WordPress user, you may run across plugins or scripts with instructions that say something like this: “This must be placed within The Loop.” That’s The Loop that you find here, so pay particular attention. Understanding The Loop arms you with the knowledge you need for tackling and understanding your WordPress themes.

The Loop is no different from any other template tag; it must begin with a function to start PHP, and it must end with a function to stop PHP. The Loop begins with PHP and then makes a request: “While there are posts in my blog, display them on this page.” This PHP function tells WordPress to grab the blog post information from the database and return it to the blog page. The end of The Loop is like a traffic cop with a big red stop sign telling WordPress to stop the function completely.

You can set the number of posts displayed per page in the Reading Settings page in the WordPress Dashboard. The Loop abides by this rule and displays only the number of posts per page that you’ve set.