WordPress templates are a collection of PHP files that dictate how content will be displayed on your site. For instance, the search.php is used to display search results.
Templates are often confused with themes. However, these two terms refer to different things and have their own functions.
A WordPress theme is what makes your website look pretty, as it determines the site’s overall design. It consists of many files that will govern how your site looks and functions, including PHP template files. In this way, templates are parts of a WordPress theme.
Getting to Know the Common Terms
When developing WordPress themes, the term “template” will have different meanings, depending on the context in which it is used.
As mentioned, generally, a WordPress template will refer to a PHP file that contains HTML, CSS, and PHP code. For better clarity, many people also call it as a template file, which will be applied to your whole site.
Some common template files include:
- home.php — used to display the front page of your site.
- single.php — this file displays a single post.
- header.php — generates the page’s header.
- sidebar.php — generates the sidebar.
- category.php — used when a user requests to display posts by category.
- comments.php — shows the comment section.
- 404.php — generates the 404 pages for when WordPress can’t find what the site visitor is looking for.
Now, there’s a specific type of template file for a single page, a page section, or a class of pages. We refer to is as a page template, which changes the look and feel of a page. You can use this file to customize the header, create an additional column, add a list of recent posts, and so on.
To retrieve and display data, template files will have WordPress functions or known as template tags. Some examples include the_content to display pages and posts, and the_title to get the title of posts from the database.
What Is the Template Hierarchy in WordPress?
Simply put, template hierarchy is the order of template files that WordPress has to go through when displaying content.
To illustrate, here’s what WordPress will do when generating your site’s front page:
- WordPress will search for the default file, which is front-page.php.
- If the first file does not exist, the software will either look for home.php or page.php. The former is used to display the latest posts, while the later for a static posts page. This will be determined by your site’s settings.
- Finally, if neither of the two files exists, then WordPress will use the index.php file — a main and universal fall-back template. WordPress always uses this file when no other file is appropriate for the given context.
A WordPress template is a PHP file that consists of HTML, CSS, and PHP code. It works within a theme to display important information of a page like the comments section, the header and footer, and search results.
To help you distinguish common terminologies, I’ve also explained the difference between template files, page templates, and template tags. Be careful when using these terms.
I hope you’ve found this article useful, and now you know the basics of WordPress templates.