Bird
Raised Fist0
Wordpressframework~10 mins

Template hierarchy in Wordpress - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Template hierarchy
Request URL
Check for specific template
front-page.php
index.php
Render Output
WordPress checks templates from most specific to general, using the first matching template file to render the page.
Execution Sample
Wordpress
<?php
if (is_front_page()) {
  get_template_part('front-page');
} elseif (is_single()) {
  get_template_part('single', get_post_type());
} else {
  get_template_part('index');
}
?>
This code chooses which template part to load based on the page type.
Execution Table
StepCondition CheckedResultTemplate ChosenAction
1Is front page?Yesfront-page.phpLoad front-page.php template
2Is single post?No-Skip single post templates
3Is category archive?No-Skip category templates
4Is page?No-Skip page templates
5Fallback-index.phpLoad index.php as fallback
6Render--Output page using front-page.php
💡 front-page.php found at step 1, so WordPress uses it and stops searching
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Template chosennonefront-page.phpfront-page.phpfront-page.phpfront-page.phpfront-page.php
Key Moments - 2 Insights
Why does WordPress use front-page.php instead of index.php?
Because front-page.php is more specific and found first in the hierarchy (see execution_table step 1). WordPress stops searching after finding the first matching template.
What happens if no specific template matches?
WordPress uses index.php as the fallback template (see execution_table step 5). This ensures something always renders.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which template is chosen at step 1?
Acategory-news.php
Bsingle-post.php
Cfront-page.php
Dindex.php
💡 Hint
Check the 'Template Chosen' column at step 1 in the execution_table.
At which step does WordPress decide to use index.php?
AStep 5
BStep 2
CStep 1
DStep 6
💡 Hint
Look for the fallback template choice in the execution_table.
If the page was a single post, which template would WordPress try to load first?
Afront-page.php
Bsingle-{post-type}.php
Ccategory-{slug}.php
Dpage-{slug}.php
💡 Hint
Refer to the concept_flow diagram showing template checks for single posts.
Concept Snapshot
WordPress Template Hierarchy:
- WordPress checks templates from most specific to general.
- It uses front-page.php for the homepage.
- For single posts, it tries single-{post-type}.php.
- If no specific template matches, it falls back to index.php.
- The first matching template stops the search and renders the page.
Full Transcript
When WordPress receives a page request, it looks for the most specific template file to use. It starts by checking if the page is the front page and looks for front-page.php. If not found, it checks for single post templates like single-{post-type}.php, then category templates, page templates, and finally falls back to index.php. Once it finds a matching template, it stops searching and uses that template to render the page. This process ensures WordPress always has a template to display, prioritizing more specific templates over general ones.

Practice

(1/5)
1. Which template file does WordPress use first when displaying a single blog post?
easy
A. archive.php
B. page.php
C. index.php
D. single-{post-type}.php

Solution

  1. Step 1: Understand single post template priority

    WordPress first looks for single-{post-type}.php to display a single post of a custom or default post type.
  2. Step 2: Recognize fallback templates

    If that file is missing, WordPress falls back to single.php or index.php, but the first choice is single-{post-type}.php.
  3. Final Answer:

    single-{post-type}.php -> Option D
  4. Quick Check:

    Single post uses single-{post-type}.php first [OK]
Hint: Single posts use single-{post-type}.php first [OK]
Common Mistakes:
  • Confusing page.php with single post template
  • Thinking archive.php is for single posts
  • Assuming index.php is always used first
2. Which of the following is the correct template file name to display a category archive for category with slug 'news'?
easy
A. news.php
B. category-news.php
C. category.php
D. archive-news.php

Solution

  1. Step 1: Identify category archive template naming

    WordPress uses category-{slug}.php to display a specific category archive page.
  2. Step 2: Match slug to template

    For category slug 'news', the template file is category-news.php.
  3. Final Answer:

    category-news.php -> Option B
  4. Quick Check:

    Category archives use category-{slug}.php [OK]
Hint: Category archives use category-{slug}.php [OK]
Common Mistakes:
  • Using archive-news.php which is invalid
  • Confusing category.php as specific slug template
  • Naming file as news.php which is not recognized
3. Given the following files in a theme: page-about.php, page.php, and index.php. Which template will WordPress use to display the About page?
medium
A. page-about.php
B. page.php
C. index.php
D. It will show a 404 error

Solution

  1. Step 1: Check for page-specific template

    WordPress looks for page-{slug}.php first for pages, so page-about.php matches the About page slug.
  2. Step 2: Understand fallback order

    If page-about.php exists, WordPress uses it before falling back to page.php or index.php.
  3. Final Answer:

    page-about.php -> Option A
  4. Quick Check:

    Page slug template overrides generic page.php [OK]
Hint: Page slug templates like page-about.php have priority [OK]
Common Mistakes:
  • Choosing page.php ignoring slug-specific template
  • Assuming index.php is used first
  • Thinking About page shows 404 without template
4. You created a template file named single-post.php but WordPress still uses single.php to display posts. What is the likely problem?
medium
A. The file name should be single-post.php but WordPress uses single-{post-type}.php where {post-type} is the actual post type slug
B. The correct file name is single-post.php but it must be in a subfolder
C. The file should be named single.php for posts
D. WordPress does not support single-post.php templates

Solution

  1. Step 1: Understand post type template naming

    WordPress uses single-{post-type}.php where {post-type} matches the post type slug exactly.
  2. Step 2: Check post type slug for 'post'

    The default post type slug is 'post', so single-post.php is correct if the post type is 'post'. But if the post type is custom or named differently, the file name must match exactly.
  3. Step 3: Identify common mistake

    If WordPress ignores single-post.php, it may be because the post type slug is not 'post' or the file is misplaced.
  4. Final Answer:

    File name must match actual post type slug in single-{post-type}.php -> Option A
  5. Quick Check:

    Template file must match post type slug exactly [OK]
Hint: Match single-{post-type}.php exactly to post type slug [OK]
Common Mistakes:
  • Assuming single-post.php always works for posts
  • Thinking file must be in a subfolder
  • Believing WordPress ignores single-{post-type}.php files
5. You want to create a custom template for the tag archive page of the tag with slug 'featured'. Which template file name should you create to follow WordPress template hierarchy?
hard
A. archive-featured.php
B. tag.php
C. tag-featured.php
D. taxonomy-featured.php

Solution

  1. Step 1: Identify tag archive template naming

    WordPress uses tag-{slug}.php for tag archive pages with specific slugs.
  2. Step 2: Match slug to template file

    For the tag slug 'featured', the correct template file is tag-featured.php.
  3. Step 3: Understand fallback templates

    If tag-featured.php is missing, WordPress falls back to tag.php or archive.php.
  4. Final Answer:

    tag-featured.php -> Option C
  5. Quick Check:

    Tag archives use tag-{slug}.php [OK]
Hint: Tag archives use tag-{slug}.php for custom tags [OK]
Common Mistakes:
  • Using archive-featured.php which is invalid
  • Confusing taxonomy-featured.php with tag template
  • Assuming tag.php is always used for all tags