Template hierarchy helps WordPress decide which file to use to show your website pages. It makes your site organized and flexible.
Template hierarchy in Wordpress
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Wordpress
index.php
home.php
single.php
page.php
category.php
404.php
archive.php
search.phpWordPress looks for templates in a specific order, starting from the most specific to the most general.
If a specific template file is missing, WordPress uses the next one in the hierarchy.
Examples
Wordpress
single-post.php
Wordpress
page-about.php
Wordpress
category-news.php
Wordpress
404.phpSample Program
This code shows a custom template for the About page. WordPress will use this file when the visitor opens the About page.
Wordpress
<?php // Template hierarchy example in WordPress theme // This is page-about.php get_header(); ?> <main> <h1>About Us</h1> <p>Welcome to our custom About page.</p> </main> <?php get_footer(); ?>
Important Notes
Always include get_header() and get_footer() to keep your site consistent.
Use template hierarchy to avoid repeating code and to organize your theme files well.
Check WordPress documentation for the full template hierarchy chart to understand all possibilities.
Summary
Template hierarchy tells WordPress which template file to use for different pages.
It helps you create custom layouts for posts, pages, categories, and errors.
Following the hierarchy keeps your theme organized and flexible.
Practice
1. Which template file does WordPress use first when displaying a single blog post?
easy
Solution
Step 1: Understand single post template priority
WordPress first looks forsingle-{post-type}.phpto display a single post of a custom or default post type.Step 2: Recognize fallback templates
If that file is missing, WordPress falls back tosingle.phporindex.php, but the first choice issingle-{post-type}.php.Final Answer:
single-{post-type}.php-> Option DQuick Check:
Single post usessingle-{post-type}.phpfirst [OK]
Hint: Single posts use
single-{post-type}.php first [OK]Common Mistakes:
- Confusing
page.phpwith single post template - Thinking
archive.phpis for single posts - Assuming
index.phpis 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
Solution
Step 1: Identify category archive template naming
WordPress usescategory-{slug}.phpto display a specific category archive page.Step 2: Match slug to template
For category slug 'news', the template file iscategory-news.php.Final Answer:
category-news.php-> Option BQuick Check:
Category archives usecategory-{slug}.php[OK]
Hint: Category archives use
category-{slug}.php [OK]Common Mistakes:
- Using
archive-news.phpwhich is invalid - Confusing
category.phpas specific slug template - Naming file as
news.phpwhich 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
Solution
Step 1: Check for page-specific template
WordPress looks forpage-{slug}.phpfirst for pages, sopage-about.phpmatches the About page slug.Step 2: Understand fallback order
Ifpage-about.phpexists, WordPress uses it before falling back topage.phporindex.php.Final Answer:
page-about.php-> Option AQuick Check:
Page slug template overrides generic page.php [OK]
Hint: Page slug templates like
page-about.php have priority [OK]Common Mistakes:
- Choosing
page.phpignoring slug-specific template - Assuming
index.phpis 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
Solution
Step 1: Understand post type template naming
WordPress usessingle-{post-type}.phpwhere {post-type} matches the post type slug exactly.Step 2: Check post type slug for 'post'
The default post type slug is 'post', sosingle-post.phpis correct if the post type is 'post'. But if the post type is custom or named differently, the file name must match exactly.Step 3: Identify common mistake
If WordPress ignoressingle-post.php, it may be because the post type slug is not 'post' or the file is misplaced.Final Answer:
File name must match actual post type slug insingle-{post-type}.php-> Option AQuick 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.phpalways works for posts - Thinking file must be in a subfolder
- Believing WordPress ignores
single-{post-type}.phpfiles
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
Solution
Step 1: Identify tag archive template naming
WordPress usestag-{slug}.phpfor tag archive pages with specific slugs.Step 2: Match slug to template file
For the tag slug 'featured', the correct template file istag-featured.php.Step 3: Understand fallback templates
Iftag-featured.phpis missing, WordPress falls back totag.phporarchive.php.Final Answer:
tag-featured.php-> Option CQuick Check:
Tag archives usetag-{slug}.php[OK]
Hint: Tag archives use
tag-{slug}.php for custom tags [OK]Common Mistakes:
- Using
archive-featured.phpwhich is invalid - Confusing
taxonomy-featured.phpwith tag template - Assuming
tag.phpis always used for all tags
