HomebaseWorks
Lesson 3 of 4

Child themes, the template hierarchy and functions.php

Customise a theme without losing changes on update, know which template file WordPress picks, and add hooks safely.

EWritten by the HomebaseWorks editorial team · reviewed 26 Oct 2026

The first customisation request on a WordPress job is usually small: change a colour, shorten excerpts, add a banner to one page type. Doing it in the parent theme means the next update erases it. A child theme is the standard way to keep changes, and knowing the template hierarchy tells you which file to override.

Advertisement

Creating a child theme

A child theme is a folder in wp-content/themes with two files. The style.css header must name the parent's folder in Template, matching exactly.

/*
Theme Name: Storefront Child
Template: storefront
Version: 1.0
*/

Then functions.php loads the parent stylesheet followed by the child's own:

<?php
add_action( 'wp_enqueue_scripts', 'storefront_child_styles' );
function storefront_child_styles() {
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
    wp_enqueue_style(
        'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array( 'parent-style' ),
        wp_get_theme()->get( 'Version' )
    );
}

Note the two functions: get_template_directory_uri() always points at the parent, get_stylesheet_directory_uri() at the active (child) theme. Mixing them up is a frequent interview question.

Which template file loads

WordPress walks a list from most specific to least specific and uses the first file it finds, checking the child theme before the parent. Three examples you should be able to recite:

  • A page with slug about and ID 12: page-about.php, then page-12.php, then page.php, then singular.php, then index.php.
  • A single post: single-post.php, then single.php, then singular.php, then index.php.
  • The category news: category-news.php, then category.php, then archive.php, then index.php.

To override a template, copy it from the parent into the child with the same name and edit the copy. Block themes use the same idea with HTML files in a templates folder and a theme.json for styles.

Actions and filters

An action lets you run code at a moment, such as when scripts are enqueued. A filter lets you change a value before WordPress uses it and must return the value. Both go in the child's functions.php, which loads before the parent's, so the parent's functions still run.

add_filter( 'excerpt_length', function ( $length ) {
    return 30;
} );

add_action( 'wp_footer', function () {
    echo '<p class="site-note">Updated weekly.</p>';
} );

Common mistakes and a practice habit

  • Copying the parent's whole functions.php into the child, which causes duplicate function errors.
  • A Template value that does not match the parent folder name, so the child shows as broken.
  • Forgetting to return a value from a filter, which blanks the output.
  • Putting business logic such as post types in the theme; it vanishes when the theme changes. Use a plugin.

Practice habit: on a local install, create a child theme, override single.php with one visible change, then switch themes and back to confirm the child survives. Do it twice until it takes under ten minutes.

Advertisement
Continue with WordPress4 lessons · 1 interview set · 1 test · 1 certificate
Not tested

More in Web and app