CAll Us: +1 888-999-8231 Submit Ticket
What Is The WordPress Loop?

What Is The WordPress Loop?

What Is The WordPress LoopWordPress is written in PHP, a programming language frequently used on the web. PHP code generates the HTML pages sent to a browser by querying the database to gather content and combine it with templates. From this, a final output is generated and displayed to users.

This is why WordPress is so powerful. In addition to being a content management system, WordPress is a framework: it provides functions and APIs that developers can use to build a website. In essence, a WordPress theme is a bundle of code that takes advantage of this framework.

Developing Themes with the Loop

One of the most important of the APIs provided by WordPress is The Loop. The Loop controls how content is displayed on a page. It’s used to display post listings, single posts, and pages.

It’s important to understand that a WordPress site doesn’t have only one loop. In fact, every major PHP file in a theme is a template file with a loop. This allows for WordPress developers to create unique page types that exploit their own Loop attributes.

The rest of a theme comprises CSS files and PHP files with functions that are imported into the template files.

An Example of a WordPress Loop

A basic WordPress loop looks like this:

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

<h1 class="post-title">
<?php the_title();; ?>
</h1>

<div class="post-content">
<?php the_content(); ?>
</div>

<?php endwhile; else : ?>
<?php endif; ?>


There is quite a lot going on here, so we’ll explain in sections:

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

This line begins The Loop with an if statement that checks to see whether there are any posts to loop through. Then it begins a while loop, starting The Loop proper. The code between this line and the end of the while loop will be repeated for each post (which might only be one post, depending on which file this code appears in and the URL being accessed).

Adding Content to The Loop

In order to add content to a Loop, you need to mark it out using HTML with embedded PHP. You can add information like post titles, descriptions, content, dates, authors, and more. You can also include fixed repetitive content if you prefer a certain style.

<h1 class="post-title">
<?php the_title(); ?>
</h1>

This section contains HTML with an embedded PHP function for inserting the post title. In the code, this PHP function is called the_title, which is a template tag provided by WordPress.

Template tags are used to display information from the database. Here, the post title of the current post is inserted. The next few lines of code are similar, but the the_content template tag is used to insert the post’s content.

WordPress provides several hundred template tags, and themes are largely constructed from HTML, the output of template tags, and other PHP functions provided by WordPress or created by the developer. A typical WordPress template file is longer and more complex than this simple example, but works on the same principle.

To see The Loop in use, look inside your WordPress installation’s wp-content/themes directory. The following files are likely to include The Loop:

  • index.php is the base template file that is used as a fallback if a specific template file is not present.
  • home.php is the home page template. By default it lists the blog’s posts.
  • single.php is the template for individual blog posts.

There are several other templates that might be present.

With a basic understanding of The Loop, template files, and template tags, you should be able to make minor changes to your WordPress theme, but before you do, read this post that discusses how to create a child theme.

Posted in:
WordPress

Source link

About the Author

Leave a Reply