0
0
Wordpressframework~5 mins

Template hierarchy in Wordpress

Choose your learning style9 modes available
Introduction

Template hierarchy helps WordPress decide which file to use to show your website pages. It makes your site organized and flexible.

You want to customize how your homepage looks differently from other pages.
You need a special layout for blog posts but a different one for pages.
You want to show a unique design for posts in a specific category.
You want to create a custom error page when a visitor lands on a missing page.
You want to control how archives or search results appear on your site.
Syntax
Wordpress
index.php
home.php
single.php
page.php
category.php
404.php
archive.php
search.php
WordPress 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
This template is used for a single blog post of the type 'post'.
Wordpress
single-post.php
This template is used for the page with the slug 'about'.
Wordpress
page-about.php
This template is used for posts in the 'news' category.
Wordpress
category-news.php
This template is used when a page is not found (error 404).
Wordpress
404.php
Sample 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(); ?>
OutputSuccess
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.