Lesson 4: Hierarchy of templates Wordpress




According to the page called, WordPress will use one of the existing templates. But which one will have priority?

From global to specific
                                                The most important image of this tutorial:


This schema defines the way that WordPress borrows to select the template to use. As soon as one of them is, it is used and the path ends.

For example, the user wants to view the April 2010 Archives: blog.Net-explain/2010/04.

1.WordPress enters the loop at archive page because it is an archive page.
2.Then it branches off to Date Archive because it is a dated archive.
3.Here we wrote / 2010/04. So this is a monthly archive.
4.WordPress then falls on date.php and use it if it exists.
5.If date.php does not exist, use the template WordPress archive.php.
6.If it does not exist, it will use index.php.

Since index.php is the last resort, it is essential.

Many people confuse home.php and index.php because a theme can be changed from the first but not the second. Indeed, it is convenient to use index.php as homepage. That way, if a template does not exist, it displays the home page.
But we must understand the difference: home.php is the template of the home page, index.php is the default template.

Globalize
If you see that the templates date.phpcategory.php and tag.php are identical, you can delete them and create instead a archive.php file. Thus, you'll save time on maintenance if you want to change some of the code.

You can go further by using only PHP index.php with some conditions in the code, but we'll see about that later.

Attention when I say that the templates are identical, this does not mean that they display the same thing on the site, they just use the same code. Their structure is identical.
For example, date.php can have a header, the range of tickets and a footer. category.php can have exactly the same code, the same structure. This is WordPress, which will be smart enough to display different notes in both cases (according to the date in one case, according to the chosen category from the other).

Specify
In the other direction, it is possible for the same template to create a more specific version. For example, you want a different page for the "Hardware".
Instead of using the template or archive.php category.php, we can create a specific template: category-hardware.php.

This method of very specific templates will prove very useful for creating templates page (WordPress Pages). We'll see about that later too.


Previous
Next Post »