Optional template files

WordPress uses what’s known as the “template hierarchy” to figure out which template file to use when displaying a specific type of content. This graphic comes from the WordPress template hierarchy page (interactive version of WordPress template hierarchy) in the WordPress developer handbook, and it shows you the entire flowchart of the process of selecting which template file to use.

WordPress template hierarchy diagram (see previous link for interactive version)
WordPress Template Hierarchy chart

I’m showing you this because, at the very end, on the right hand side here, you can see index.php. This is the fallback file for all template files, so if no other template files exist, WordPress automatically falls back to index.php as the main template file. That’s what WP Rig does out of the box. It provides index.php as the only template file in use. However, as you’ve seen previously in the course, WP Rig also ships with optional template files directly from the template hierarchy, you can drag and drop into your theme and they automatically take effect. Those template files sit under the folder called “optional“.

Template files in optional directory

  • archive.php
  • attachment.php
  • category.php
  • custom-page-template.php
  • custom-post-template.php
  • front-page.php
  • search.php
  • single.php
  • singular.php

Here you have template files for the most common things people want to do in the template hierarchy. Things like a template for single.php, that would be a single post. A template for page.php, that would be a regular page. A template for singular pages, that would be pages or posts. We have archive, and attachment, and category, and a custom page template, a custom post template, and even a front page template.

The idea here is, all of these optional template files have been created so that they are quite opinionated about how to display content. They are different from what index.php would do, and give you a starting point as you start extending your theme. The idea here is, as you build up your own theme, whenever you run across a situation where you need a custom template for just one scenario, say only single posts, they maybe need some additional content, or only pages need a certain type of styling, or you need to customize the search output, or maybe you need a custom template for a specific category, or all categories. In those circumstances, you can go into the optional folder, grab whatever file matches roughly what you want, and just drag and drop it into the root folder. That template file then automatically takes effect and takes effect in place of index.php for that particular template scenario, and then you can use that template file to customize the experience further.

I recommend you take some of these files and dump them into the root of the folder, just to see what happens and see how they work differently. They can be useful, but they’re not always necessary, so think carefully about how you do things here. First, make sure that you use modularity as much as possible to create the output you want without adding additional template files, then if you can’t make those modules work the way you want to, then consider adding in one of these optional template files to give you that extra level of control of a specific display, and if that doesn’t work, then you can create your own custom template files based on either the optional ones, or based on the template hierarchy you see here.

More from this course: