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.
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
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.
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 http://www.themelab.com/?p=1
- – 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 php.net
- – 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 WordPress.org. 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.
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></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.
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.
There are a couple of advanced techniques that are on the list Template hierarchy Page on WordPress.org.
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.