A theme consists of many files, this can get very confusing very fast. What files are needed? What files runs when and why? Here are the explanations and tips about WordPress template files.
First things first, what is a template exactly? A template is a PHP file that is a part of a theme. For example, there is a template for displaying the home page, one for displaying pages and another one for displaying posts.
What Are The Existing WordPress Tempaltes?
Here is a list of the existing WordPress templates that can be used in themes:
- index.php – The master template. If a certain template file does is not found, this one is being used.
- home.php – Displays the home page.
- page.php – Displays page’s page (It displays my contact page).
- single.php – Displays the post’s page (This is what you are now viewing if you are reading this here).
- author.php – Displays the author page. This is not such a common template as most themes don’t use author pages.
- tag.php – Displays the posts which have a specific tag. (e.g. FTP)
- category.php – Displays posts from a specific category. (e.g. WordPress)
- date.php – Displays posts from a specific time range.
- archive.php – Used if tag.php,category.php or date.php is not found.
- search.php – Displays search results. (e.g. How To)
- 404.php – When WordPress sends a 404 header. (When it cant find something)
- image.php,video.php,audio.php,application.php or any other first part of the attachment’s MIME type – Attachment.
- attachment.php – If one of the last templates (image.php…) are not found.
Here is a very useful image provided by WordPress Codex (click to enlarge):
Custom Templates
There are two main types of custom WordPress templates.
The first one are templates that WordPress looks for automatically, and if one is found it will be used. What the heck am I talking about?
If you have a category called “News” with the ID of 512 and you want the archive page of that category to have it’s own unique template you can create a new template file called category-512.php
in the theme’s folder. And WordPress will use it.
This is the list of custom wordpress templates you can use:
- category-id.php
- tag-slug.php
- pagename.php
- firstpartoftheattachmentsMIMEtype.php
Now for the second type of WordPress custom templates.
Let’s say you have more then one page* you want to be displayed in a different way then the others. How do you do that?
Create a new file called whatever.php
at your theme’s folder and inside write whatever you want instead of single.php
or index.php
(depending what displays the single posts at your theme).
Add this comment to the top of the file:
<?php
/*
Template Name: The Whatever Template Yo
*/
?>
(Of course change The Whatever Template Yo
to the new template’s name)
Now edit (or create) the pages which you want to use this template and in the right side pick the desired template from the drop down menu. Just like in the screenshot at the right side.
*If you want to do this for posts rather then pages then you will find the Custom Post Template plugin very useful.
Any questions/corrections? Comments are always welcome.