What is the template hierarchy in WordPress?

When a page is requested, WordPress finds out its type (single post, page, category and so on) and looks for a template file in a fixed order, from the most specific name to the most general one. The last choice is always index.php.

PageOrder of files
Single postsingle-{post-type}-{slug}.php, single-{post-type}.php, single.php, singular.php, index.php
Pagepage-{slug}.php, page-{id}.php, page.php, singular.php, index.php
Categorycategory-{slug}.php, category-{id}.php, category.php, archive.php, index.php
Front pagefront-page.php, then home.php or page.php, index.php
404404.php, index.php

You can test it with a theme where each file prints its own name. We did exactly that in WordPress Template Hierarchy. In block themes the same logic is used, but with files like templates/single.html.


Learn more about the similar topics:
Tutorials
WordPress Themes
WordPress Top Theme List
WordPress Theme Files and Functions
WordPress Template Hierarchy
WordPress Enqueue Scripts and Styles
WordPress Loop
WordPress WP_Query Class
Exercises & Assignments
Build and activate a minimal classic theme
Show the three latest posts with WP_Query
Load a stylesheet with wp_enqueue_style and a version
Show a message only on the front page
Create a child theme which changes the footer
Interview Questions & Answers
What is a WordPress theme?
What are commonly used WordPress theme functions?
Which function is used to get the home URL in WordPress?
Which are the necessary files for a WordPress theme?
What is the use of the wp_head() function in WordPress?
What is the WordPress Loop?
What is the difference between is_home() and is_front_page()?