WordPress templates: template archives – .com

In this tutorial we see one of the two types of WordPress templates: Template files, which are organized through hierarchical nomenclature.

WordPress templates

First of all, we are going to clarify a concept, and that is that WordPress comes from the American world, where they use a slightly different terminology, which can cause confusion here.

When in this article I talk about templates, I don’t mean themes (topics), that is, the themes that can be changed in the “Appearance” menu, but rather the templates within each theme. Basically in themes, there are two types of template:

  • Template files, which I cover in this tutorial
  • Page templates, which I’ll cover in tomorrow’s tutorial

The template files are those that affect any URL of our WordPress based on the type of information that we are seeing. Thus, they can affect (among others) the:

  • posts
  • pages
  • Categories
  • tags
  • files by dates
  • Main page
  • blog page
  • comment pages
  • subpages
  • Etc.

These templates will be assigned automatically, due to the mere fact of the very nature of that information. So, we don’t have to go anywhere to tell you which template to use, but WordPress will assign it to you automatically. We’ll see it in a moment.

On the other hand, we have page templates. This refers only to the templates that some themes have available to be chosen from a dropdown when an editor or webmaster creates a “page” in WordPress. We’ll see you tomorrow.

That being said, let’s focus now on the former.

Template files (File Templates)

The template files are nothing more and nothing less than a PHP file that is usually found in the root folder of the theme or sometimes in the /templates/ directory of the theme (although it can be found in another folder) that indicates what each type of view should look like in our WordPress.

For example, let’s imagine that we want to modify the appearance of EVERYBODY our posts, changing the size of their title. Or the background color of the page. Or removing the publication date. It doesn’t matter, what you want. Well, we can do that in a centralized way by modifying a single file. The file single.php.

See also  SEO course #9. Google Search Console and RankBrain - .com

And what if we want to do the same with pages? In other words, modify them all so that they do not have the date or the author, for example. Well, we will do that by modifying the file page.php.

What do you want to modify both at the same time? No problem, we have the template file singular.phpwhich works for both at the same time.

I suppose that you have already caught the trick and you will not be surprised if I tell you that the file category.php controls the template of the category pages, the file tag.php tag pages, or even author.php for the authors page.

So… How many types of template files are there? Uh, well. But more than memorizing a list, the most comfortable thing is that you print this graph:

Well, there is the hierarchical classification of the templates. And what do I mean by that hierarchical classification? Hierarchical in what sense? Well very simple. This hierarchy prevents two files targeting the same content type from colliding.

Hierarchy of template files in WordPress

Continuing with the example from before, let’s imagine that I want to modify the appearance of the posts, so that they have a page background of a different color than the rest of the web. As we have said, you should modify the file single.php, TRUE? But I have also said that the file singular.php includes both posts and pages, right?

So who is WordPress going to listen to, if both indicate contrary things? Well, in this case the more specific template file will always win. In this case single.phpsince it is more specific than singular.phpwhich covers more things.

And would we have something more concrete than single.php? Of course! Would single-slug.php, where “slug” would be the slug of that particular post. In fact, the hierarchy would work like this:

  • If the file exists single-{slug}.php, this one is used. If it doesn’t exist…
  • …the file is used single.php. If it doesn’t exist…
  • …the file is used singular.php. If it doesn’t exist…
  • …it’s used index.php.
See also  Music monetization course #5. Sound resources and library music - .com

Attention to detail. Notice that I say “If the file exists”. That means that in reality, all of this is optional. Yes single.php does not exist, no problem, will be used singular.php. And if it doesn’t exist… index.php, which in fact, is the only template file required. In fact, a theme could consist of only one file index.php and a file style.css and nothing else, since index.php it is the last spring, so to speak. Obviously, the more template files a theme has, the richer it will be, since each type of information (articles, pages, categories, author pages, etc.) It will show itself differently, and with its own personality.

The same happens with the pages, in which the hierarchy is:

  • If the file exists page-{id}.php, this one is used. If it doesn’t exist…
  • …the file is used page-{slug}.php. If it doesn’t exist…
  • …the file is used page.php. If it doesn’t exist…
  • …the file is used singular.php. If it doesn’t exist…
  • …it’s used index.php.

The categories? The same!

  • If the file exists category-{id}.php, this one is used. If it doesn’t exist…
  • …the file is used category-{slug}.php. If it doesn’t exist…
  • …the file is used category.php. If it doesn’t exist…
  • …the file is used archive.php. If it doesn’t exist…
  • …it’s used index.php.

Notice that in this case, the step prior to the use of index.php it is archive.phpwhich would be the equivalent of singular.php but for views in which instead of a single post, page or any content, we have several (categories, tags, author page, archives by dates, etc). All those always show several contents, not just one. But instead of calling it “plural.php”, they called it “archive.php” and that’s it.

See also  iMovie course - .com

All this is also applicable to CPTs (Custom Post Types), which are those contents that we add through code, themes or plugins. For example products, testimonials, portfolios… in this case the structure is:

  • If the file exists single-{custompostotype}-{id}.php, this one is used. If it doesn’t exist…
  • …the file is used single-{custompostotype}-{slug}.php. If it doesn’t exist…
  • …the file is used single-{customposttype}.php. If it doesn’t exist…
  • …the file is used single.php. If it doesn’t exist…
  • …it’s used index.php.

By the way, if you want to know how to make your own CPT, take a look at the .

Ok, so we can have all these optional files and many more just by copying index.phpchanging the name to the one that interests us, and modifying the content to our liking.

Throughout this article I have left you several diagrams of the template files that you should print and place on the wall (or even as wallpaper 😉. Choose the one you prefer, or even take a look at with links to the codex 🙂

conclusion

The WordPress template file system is the best out there. Just copying any existing file (for example index.php) and changing its name to a more specific one (for example page.php) We already have it ready to be modified directly.

WordPress will already detect that it exists, WordPress will already detect that it has priority for being more specific, WordPress will already detect that someone is looking at a page, and WordPress will already use it to display the information. What more do you want? A video? Well, in that case take a look at , in which we will see this and much more 🙂

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x