Right now the Shimaenagado theme displays every page with a single index.php. But in the finished form, we need different looks for the top, About, announcement list, and article detail. That’s where WordPress themes’ biggest rule comes in — the template hierarchy. It’s a correspondence table saying “for this URL, this file is in charge.”
How it works: WordPress searches by file name
Each time a page is opened, WordPress searches the theme folder in a fixed order of priority. It searches like this: “if a specialist handler exists, use them; if not, a more general handler; if no one at all, index.php.”
For example, for the detail page of a post (an announcement article):
Is there single.php? → if so, adopt it
↓ if not
singular.php? → if so, adopt it
↓ if not
index.php (the last line of defense)When we earlier said “index.php is the last line of defense,” it was because it’s the endpoint of this way of searching.
The table of handlers used on a company site
The full template hierarchy is quite large, but the ones you’ll use first in practice are just these. Let’s memorize them by matching them to the finished Shimaenagado.
| Page | First-choice file in charge |
|---|---|
| Top page | front-page.php |
| Static page (About) | page.php |
| Post list (announcement list) | home.php |
| Post detail (announcement article) | single.php |
| Category-specific list | category.php |
| Search results | search.php |
| Not-found page | 404.php |
| Last line of defense | index.php |
There are more specialized handlers too
The hierarchy has the property that “a more specific name takes priority.” For example:
page-company.php— a handler only for the static page whose slug iscompany(takes priority over page.php)single-shohin.php— a handler only for the detail of post typeshohin(we’ll use this later in the custom post type chapter)archive-shohin.php— a handler only for the list of post typeshohin
“A big net (index.php) → a net by kind (single.php) → a net that names it specifically (single-shohin.php)” — gradually finer nets take priority. Once you grasp this feeling, you don’t need to memorize the whole hierarchy table.
Summary of this lesson
- The template hierarchy = WordPress’s rule for searching for a file in charge for each kind of page. If no specialist handler exists, a more general handler → finally
index.php - On a company site, the ones you’ll use first are the six front-page / page / home / single / category / 404, plus index
- “A more specific name takes priority” — you can also make specifically-named handlers like
single-shohin.php