Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

WordPress Loop: The Mainstay of Your Content Display

June 17, 2015

4 Min Read
wordpress loop
Reading Time: 4 minutes

The code that displays the blog posts on your WordPress theme is widely known as the WordPress Loop among WordPress users and as The Loop on WordPress Codex.

The Loop is considered as the most important part of WordPress theme. It is responsible for displaying blog posts on the current page based on the parameters.

In this guide, we will go over the basics of WordPress Loop with examples so that you get a clear understanding of what the code does, how to customize it and where it can be found in your WordPress theme.

The Logic of WordPress Loop

To start off with the logic, let’s have a look at the basic code of the loop. Then we can go over each line to understand it.

//Check If posts exist, if yes then execute while loop

 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

//Display Posts code here

           <h2><?php the_title() ;?></h2> //Display the title of the post

            <?php the_post_thumbnail(); ?> //Display the post thumbnail AKA featured image

            <?php the_excerpt(); ?> //Display excerpt of the post

<?php endwhile; else : ?> //End the while loop

                       <p><?php _e( 'No Posts To Display.' ); ?></p>

<?php endif; ?> //end If statement

In the Loop, we have one “if” statement and a “while” loop. The “if” statement checks if there are posts available to be displayed, the “have_posts” WordPress function is a boolean one, that returns a true or false value. If there are posts found, it returns “true” and code proceeds to “while” loop.

The “while” loop also has the “have_posts” WordPress function. It returns “true” equal to the number of times we have set blog posts to be displayed in the WordPress admin.

The code then proceeds to the WordPress function “the_post”. This function sets up the post and then we can use more functions to extract elements from the posts. In the above stated example we have used:

the_title -> fetches the post title

the_post_thumbnail -> fetches the featured image of the blog post

the_excerpt -> fetches the excerpt of the blog post

There are certainly more functions that we can use to extract elements from posts, we will cover a few with examples.

Hyperlinking on Post Title

You have probably seen that blog post titles have links to the actual posts. Upon clicking on the hyperlink you are navigated to a single page view of the blog post. Most themes have a file called single.php, this file is called when you are viewing a single post.

We will use “the_permalink” WordPress function and enclose “the_title” function to link it.


<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

           <h2><a href="<?php the_permalink(); ?>"><?php the_title() ;?></a></h2>

            <?php the_post_thumbnail(); ?>

            <?php the_excerpt(); ?>

<?php endwhile; else : ?> //End the while loop

             <p><?php _e( 'No Posts To Display.' ); ?></p>

<?php endif; ?>

Displaying the Content, Author, Date, and Category Using the Loop

You might have seen many blogs with posts that show the name of the author, entire content of the main blog page, with date and category also. You can easily display all these useful details within the loop using the following functions:

the_content – Displays the full blog content

then_author – Displays the author name

the_time – Displays the time and date

the_category – Displays the category of the post

Your loop will look something like

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

           <h2><?php the_title() ;?></h2>

           <?php the_author(); ?> <?php the_time('F j, Y'); ?> <?php the_category(); ?>

           <?php the_post_thumbnail(); ?>

           <?php the_content(); ?>

<?php endwhile; else : ?> //End the while loop

             <p><?php _e( 'No Posts To Display.' ); ?></p>

<?php endif; ?>

Use of Conditional Tags

There are many tags used to check for conditions, but fortunately, in WordPress these tags are pretty much self explanatory. For example, “is_home” checks whether the current view is of the home page (main page of the blog). We can use Conditional Tags to initiate certain Loops.

For example, if we enclose our Loop between “if” condition that checks whether the current page is the main page, then the Loop will only be executed on the main page.

<?php if (is_home()) {  //Returns true if current page is main/home page

        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

         <h2><?php the_title() ;?></h2>

           <?php the_post_thumbnail(); ?>

           <?php the_excerpt(); ?>

<?php endwhile; else : ?>

          <p><?php _e( 'No Posts To Display.' ); ?></p>

<?php endif; ?>

} ?>

We can use Conditional Tags in our template files to control which Loop is executed, thus having multiple loops in a single file.

Some of the most used Conditional tags are.

is_admin() – Returns true when admin is logged in the site.

is_single() – Returns true if single post is being viewed.

is_page() – Returns true on certain pages, e.g. is_page(‘about-us’)

is_category() – Returns true on category pages, e.g. is_category(‘wordpress’)

is_tag() – Returns true on tags.

is_author() – Checks for author and Returns true. e.g. is_author(‘ahsan’)

is_404() – Returns true if page does not exist, can be used on 404 pages to execute a customized Loop

has_excerpt() – Returns true if post has a excerpt.

The Loop is the Future

Think of the Loop as the driving engine of WordPress themes. It is absolutely necessary to use it to display posts. In this post I’ve explained the basics, there are many ways a loop can be used, I recommend you to go over the Loop in action.

But, the Loop would work great if it is coupled with a high-performing fast WordPress hosting medium. Cloudways is ready for the Loop! Our tests have shown that websites on Cloudways load 100% faster.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Start Growing with Cloudways Today!

We never compromise on performance, security, and support.

Ahsan Parwez

Ahsan is the Community Team Manager at Cloudways - A Managed Cloud Hosting Platform. He loves to solve problems and help Cloudways' clients in any aspect he can. In his free time, you can find him playing RTS PC games.

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!