WordPress Loop: What is it & How to Use it?

WordPress Loop: What is it & How to Use it?
WordPress Loop: What is it & How to Use it?

When it comes to displaying your blog posts in a WordPress theme, there is one essential piece of code that every WordPress user should understand: the WordPress Loop, often simply called “The Loop” in the WordPress Codex.

The WordPress Loop isn’t just another code snippet; it’s the core of your WordPress theme, powering the dynamic presentation of blog posts on your site.

In this in-depth guide, we’ll walk you through the key aspects of the WordPress Loop, with practical examples to help you understand how it works, how to customise it for your needs, and where it fits within your WordPress theme.

What Is The WordPress Loop?

A PHP code used to display WordPress posts is known as the WordPress Loop. WordPress themes rely on this loop to showcase posts on the current pages.

The Loop works by executing a set of functions designed to retrieve and display posts. It is a fundamental part of WordPress code, incorporating various Template Tags that handle publishing, formatting, and organising post data.

By default, the Loop displays the following information for each post:

  • Title (the_title())
  • Time (the_time())
  • Categories (the_category())

When Should You Use The Loop in WordPress?

Loops in WordPress can be used whenever you need to display posts, pages, custom post types, or any other content stored in the WordPress database.

Here are some common scenarios where implementing a loop in WordPress is necessary:

  • Displaying blog posts
  • Displaying page templates
  • Displaying archives
  • Displaying search results
  • Displaying custom post types
  • Displaying a specific number of posts per page

How To Add A WordPress Loop

Here’s how you can add the WordPress Loop using simple code. The code below is quite basic and may need customisation depending on your requirements.

For now, let’s take a look at the basic Loop code. We will then go through each line to understand how it works.


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

//Display Posts code here

//Display the title of the post
//Display the post thumbnail AKA featured image
//Display excerpt of the post
//End the while loop

 

//end If statement

Code Explanation

In the Loop, we use an “if” statement and a “while” loop. The “if” statement checks whether there are any posts available to display. The WordPress function have_posts() is a boolean function, returning either true or false. If posts are found, it returns “true”, allowing the code to proceed to the “while” loop.

The “while” loop also uses the have_posts() function. It returns “true” as many times as the number of posts set to be displayed in the WordPress admin settings.

Next, the code runs the WordPress function the_post(). This function prepares the post, enabling the use of additional functions to extract specific elements from the posts. In the example above, we have used:

  • the_title() – retrieves the post title
  • the_post_thumbnail() – retrieves the featured image of the post
  • the_excerpt() – retrieves the post excerpt

There are many other functions available for extracting different elements from posts, and we will explore a few more examples.

by Peter Wootton
3rd September 2024
Tags:

I am an exceptionally technical SEO and digital marketing consultant; considered by some to be amongst the top SEOs in the UK. I'm well versed in web development, conversion rate optimisation, outreach, and many other aspects of digital marketing.

All author posts
Related Posts
75% of users never scroll past the first page of search results.
HubSpot