What is the WordPress loop –

Hosting Mautic QuickStart -50% with support in Spanish
24 hours and free training

Send up to 1,000,000 emails/year. Mautic support in Spanish 24 hours, 365 days

The WordPress Loop is a standard functionality adopted in WordPress development to display content.

If you have ever wanted to make code changes to your theme files you may have come across this functionality as Loop code is usually used as part of your theme files.

Loop can be translated as loop, and that idea of ​​loop leads us to think that this Loop functionality in WordPress would only serve to display a list of articles.

However with the Loop it is possible to display individual entries as well; in fact we can display any Custom Post Type (posts, pages, etc.) using the Loop functionality.

How is the content generated?

Before delving into the WordPress Loop, it is first necessary to understand how content is generated.

You’re probably wondering, ok, the WordPress Loop is for displaying content, but how does WordPress know what content to display? That’s where URLs come into play.

If you have noticed, different content is displayed every time the url changes in the address bar of your browser.

In summary, the process would be as follows:

    • A URL is consulted: if this url is a file, then the file is directly retrieved.
      You can check this, for example, directly consulting your file http://yourdomain.com/robots.txtyou will see that it is shown directly, that is, WordPress does not intervene in this request.
    • If the url does not refer to a specific file then possibly you have a url that refers to a page, a post, a category, etc. The url can be a friendly url or it can be a parameterized url (in case you haven’t enabled friendly urls on your site).
    • All WordPress requests go through the file index.php which is at the root of your site. From there, a series of processes are carried out to retrieve the data from the database with the parameters of the url.
  • The data is retrieved and the theme you have active is used to display it: the theme has a series of files that are used hierarchically depending on the content to be displayed.
See also  How to put an image in the summary of a post in categories

What is Theme File Hierarchy?

Having clear how the content is generated, then now we ask ourselves the question: How does WordPress know which theme file to use to display content? This is where the theme file hierarchy.

In the following image we can see the hierarchy of files that a Wor theme or template could have:

Note that a theme does not necessarily have to have all the files shown here.

The file upload priority goes from left to right, for example, if the category.php file does not exist it will look for the archive.php file, if it does not exist then it will use index.php as the last option.

Depending on the file to use, in this theme file there is a WordPress Loop logic that will serve to display the content. For example, if we are looking at the detail of an article, then possibly WordPress uses the single.php file of the theme you are using.

Loops in WordPress

In the following image we see an example of wordpress loop t in this case several entries are being displayed, one after another, in the theme it is Twenty Seventeen and the archive.php file is being used. It is in this file where there is a Loop logic.

However, as we discussed before, we’re not just using the Loop to display a list of posts, it’s also possible to display individual content.

In the following image we are showing the detail of an entry, the theme is also the Twenty Seventeen theme and the file that is being referred to is single.php. That is, the Loop logic is inside this file and returns only one input.

See also  logo in the header part of the website

WordPress Loop Codebase

The best way to understand the Loop code is to simplify it and keep only the basic code, you must also have basic knowledge of PHP programming.

The following code shows the WordPress Loop Codebase.

if ( have_posts() ) { while ( have_posts() ) { the_post(); // Content Here } }

Basically the code has two parts:

    • The first part is a check, through the have_posts() function, we check if there is content to display, if there is no content we avoid entering the loop.
  • The other part is the loop itself using PHP’s while() structure, for each loop we check again for content; inside the loop we call the_post() function which provides pointer functionality to locate us to the record with content and then we can display the content.

How do we display content?

To display content we can use WordPress functions. In the loop we have used two functions so far: have_post() and the_post(), but WordPress has many more.

The functions that go inside the WordPress loop are part of the functions that are used when building a theme. You can see a complete list of these functions in the documentation for .

Some of the functions that we can use within the WordPress loop are:

  • the_title()
  • the_content()
  • the_author()
  • the_permalink()
  • the_ID() etc

In this case these functions will print the value retrieved from the current register. If you don’t want to print it directly, each function has an equivalent, for example:

  • get_the_title()
  • get_the_content()
  • get_the_author()
  • get_the_permalink()
  • get_the_ID() etc

Real example of Loop in WordPress

When we inspect the theme files we will see that Loop’s construction at first glance differs a lot from our previous code base.

See also  The best fonts for WordPress, choose the best font

In the following code we see a real example of using a WordPress Loop to display content.

Video tutorial on the WordPress Loop

conclusion

The Loop structure is an important part that WordPress includes as part of its functionality.

I hope that it has become a little clearer to you what it is and how it works and if at any time you need to modify something, you already know that you first have to locate the file of your theme, locate the Loop code and within it use the different functions of template tags to display your information.

Help us improve our content by sharing your opinion

WordPress technical support at and regular speaker at Meetups and WordCamps in America.

Loading Facebook Comments ...
Loading Disqus Comments ...