The ultimate guide to the WordPress loop

In this tutorial, I’ll go through it The bow, and how WordPress uses it to display your posts and pages. Remember this is a bit more advanced than our previous tutorials. Hopefully you will find it ultimate guide easier to understand than what’s in the WordPress documentation. Here’s what we’re going to go through:

  • A definition of The bow.
  • Basic flow of The Loop.
  • Template tags that are used in The bow
  • What should I do after The bow
  • Template file hierarchy

If you want to better understand how a WordPress theme really works behind the scenes, keep reading.

Note: This tutorial assumes that you are using a standard WordPress theme, e.g. B. the standard Kubrick theme. No advanced multi-loop stuff … yet.

What is the loop?

You’re probably still wondering what The Loop is all about. Basically, this is what the content is displaying on your. see Home page, Her individual contributions, pages, Archives, search results and more.

By default, when a user accesses your homepage, archives or search results, the loop shows a certain number of posts defined in your reading options.

WordPress reading options

At the moment mine Home page shows 10 posts per page, that’s what I’ve defined Show at most * posts. On individual posts and pages – the same basic loop code is just displayed only this particular page.

Basic flow of the loop

Let’s split the loop into 3 parts.

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

1. What should be displayed in the loop

<?php endwhile;?>

2. What is displayed when the loop ends

<?php else : ?>

3. If nothing is displayed

<?php endif; ?>

if If there are posts available in the query, they will be displayed in one while Loop what’s in. Are defined Part 1. When the time is up it will show what’s inside Part 2. If no posts were found or another 404 error occurs, part 3 is displayed.

Template tags used within the loop

Unless you want to 1. What should be displayed in the loop Repeated 10 times on your WordPress blog homepage, you should probably learn some of the basic template tags. Let’s take a look at the code from index.php in the standard WordPress template.

WordPress Loop Breakdown

As you can see, there is a lot Template tags inside the loop that prints things like the post title, permalink, content, etc. I’ll break down each of the template tags in the standard WordPress theme.

  • – This will repeat the post’s permalink, ie
  • – This reflects the post title, ie hello world!
  • – This will reflect the date i.e. April 4, 2008. For a full list of ways to format the date, see
  • – This will show the author’s name, ie Leland. This is commented out in the standard design.
  • ‘); ?> – This will separate the tags assigned to the post with commas followed by a line break
  • – This will display the categories in a similar way to the tags above.
  • – The link to edit the post is only visible to people with the appropriate permission.
  • – Displays the link to the comments. This will not be shown on individual posts or pages.

There’s a lot more on that Template tags Page on Some of these may work in the loop and some may not.

After the loop

Let’s take a look at the code after the loop in the standard design has finished looping.

After the loop breakdown

<div class="navigation">
<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>

As you may have guessed, it shows the pagination that you see on the home page, in the archives, and in search results. These are not displayed on individual posts and pages. Of course you could replace that with something like that PageNavibut that’s up to you.

If there are no posts to display (possibly due to a 404 error), the following will appear after that different

<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>

That will show them Not found Message along with the search form. In this case a search form code would need to be in a file named searchform.php in the template directory, which is located in the standard theme.

Template hierarchy

Some template files take precedence over index.php for certain page types if they are present in the template directory. The following are some examples of the template hierarchy, ordered by priority.

Home page

  1. home.php
  2. index.php

Single contribution

  1. single.php
  2. index.php

Search results

  1. search.php
  2. index.php

404 page

  1. 404.php
  2. index.php

There are a couple of advanced techniques that are on the list Template hierarchy Page on

So what’s the point of the template hierarchy? Basically, you can use it to create new layouts for different types of WordPress sites without hacking yours index.php File too much.

Wordpress Free Download Nulled Themes, PHP Templates, Script, Download HTML Nulled Script and Shopify, E-Commerce Plugin Free CodeCanyon Free Download Nulled Scripts. Download PHP Nulled Scripts and PHP Website Clone Scripts Nulled From CodeCanyon, ThemeForest, Download All New Templates Plugins ThemeForest Null...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button

Adblock Detected! covers its costs with advertising revenues. Please turn off the ad blocker to support us!