๐ฏ By the end of this lesson you canโฆ
- Predict which template WordPress uses
- Create specific templates (single, page, archive, 404)
- Use the most specific file only when needed
| Visitor sees | WordPress tries (in order) |
|---|---|
| A single post | single-{post_type}-{slug}.php โ single-{post_type}.php โ single.php โ singular.php โ index.php |
| A page “about” | page-about.php โ page-{id}.php โ page.php โ singular.php โ index.php |
| Category “news” | category-news.php โ category.php โ archive.php โ index.php |
| Search results | search.php โ index.php |
| Not found | 404.php โ index.php |
| Home page | front-page.php โ home.php (blog) or page.php โ index.php |
The lookup itself is simple logic โ write it:
๐ ๏ธ Pick the template
Write pickTemplate($candidates, $files): given the ordered list of candidate names and the files your theme has, return the first candidate that exists (or index.php).
๐ Key takeaways
- Most specific wins:
single-product.phpโsingle.phpโsingular.phpโindex.php. - Pages:
page-about.phpโpage.php; categories:category-news.phpโcategory.phpโarchive.php. - Start with index.php; add specific templates only when a page needs to look different.
Finished reading & practising?
Saved in this browser. Create a free account to keep it forever.