0
0
Wordpressframework~10 mins

Template hierarchy in Wordpress - Step-by-Step Execution

Choose your learning style9 modes available
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.