Themes & Template Hierarchy

45-60 minIntermediate

A WordPress Theme controls the visual presentation of your site. Understanding the Template Hierarchy (how WordPress chooses which template file to use) is essential for custom development. Child themes let you customize safely without losing changes when the parent theme updates.

Prerequisites

  • Access to theme files via FTP or Theme Editor
  • Basic PHP and HTML knowledge
  • Understanding of WordPress file structure

Step-by-Step Guide

1

Understand Theme Essentials

1

A theme is a collection of files that produce the graphical interface

2

Minimum required files: style.css (with theme header) and index.php

3

style.css: Contains theme metadata (name, author) plus CSS styling

4

index.php: Fallback template used if no more specific template exists

5

functions.php: Theme-specific PHP functions, hooks, and enqueues

2

Master the Template Hierarchy

1

WordPress uses a decision tree to choose which template file to load

2

It tries the most specific file first, falling back to more general files

3

Single Post: single-{post-type}-{slug}.php → single-{post-type}.php → single.php → index.php

4

Pages: page-{slug}.php → page-{id}.php → page.php → index.php

5

Category: category-{slug}.php → category-{id}.php → category.php → archive.php → index.php

6

Home: front-page.php (static) OR home.php (blog posts) → index.php

Memorize this pattern: WordPress tries specific first, then falls back to general
3

Create a Child Theme

1

A child theme inherits from a parent theme but allows safe customizations

2

Create a new folder in wp-content/themes/ named like: parentname-child

3

Create style.css with the required header (Template: must match parent folder name)

4

Create functions.php to enqueue both parent and child styles

5

Activate the child theme in Appearance > Themes

Example
/* style.css - Child Theme Header */
/*
Theme Name: Twenty Twenty-Four Child
Template: twentytwentyfour
Description: Child theme for Twenty Twenty-Four
Author: Your Name
Version: 1.0
*/

/* Your custom CSS goes below */
4

Enqueue Parent Styles Correctly

1

Add this to your child theme functions.php

2

This ensures parent styles load before child styles

3

Child styles automatically override matching parent selectors

Example
<?php
// functions.php - Child Theme Setup
add_action( 'wp_enqueue_scripts', 'my_child_theme_enqueue_styles' );
function my_child_theme_enqueue_styles() {
    // Load parent theme stylesheet
    wp_enqueue_style( 
        'parent-style', 
        get_template_directory_uri() . '/style.css' 
    );
    
    // Child style.css loads automatically after
}
get_template_directory_uri() = parent theme, get_stylesheet_directory_uri() = child theme
5

Override Template Files Safely

1

To customize a parent template, copy it to your child theme

2

WordPress will automatically use the child theme version

3

Example: Copy twentytwentyfour/single.php to twentytwentyfour-child/single.php

4

Edit only the child theme copy

5

Parent updates will not overwrite your customizations

Only copy files you need to modify - inherit everything else

Verification Checklist

  • Understand minimum theme requirements (style.css, index.php)
  • Can explain the template hierarchy for posts, pages, and archives
  • Child theme is set up with correct style.css header
  • Parent styles are properly enqueued in child functions.php
  • Can override parent templates by copying to child theme

Pro Tips

  • Use the Template Hierarchy diagram on developer.wordpress.org as a reference
  • Install the "What The File" plugin to see which template is being used
  • Child themes are THE single most important best practice for custom work
  • Template parts (get_template_part) let you reuse code across templates
  • Block themes use HTML templates instead of PHP - different hierarchy rules apply