Custom WordPress Themes

How, and more importantly why and when, to create a custom WordPress theme, starting with a very basic example for easy learning.

  • A custom WordPress theme can start very simply: create a folder in /wp-content/themes/ with a style.css (containing a special header comment with the theme name), plus template files like index.php, header.php, footer.php, page.php, and functions.php.
  • index.php is the only template a theme is required to have; it acts as the fallback for every type of page when no more specific template exists.
  • Key optional templates include front-page.php (homepage), post.php (posts), page.php (pages), archive.php/category.php/tag.php (filtered lists), search.php (search results), and 404.php (not found), plus screenshot.png for the theme preview.
  • The template hierarchy is the core concept: WordPress identifies the page type, builds a list of template names from most specific to most general, and uses the first file it finds in your theme.
  • Example: a category with slug 'unicorns' and ID 4 looks for category-unicorns.php, category-4.php, category.php, archive.php, then index.php.
  • The homepage depends on Settings > Reading: front-page.php always wins if it exists, home.php lists blog posts, and page.php is used when the front page is a static page with no front-page.php.
  • Custom post types follow the same pattern (e.g., single-book.php and archive-book.php, falling back to single.php and archive.php).
  • In child themes, a file in the child replaces the same file in the parent, but more specific files still win (a child's category.php does not beat a parent's category-unicorns.php).
  • To debug any theme, determine the page type and walk down its template list until you hit a file that exists; you only need to create a more specific template when you want a page to look different.
  • Block themes use .html files instead of .php, but the hierarchy works the same way. A basic example theme is available in a public GitHub repo.

Custom WordPress themes can and typically are complex beasts, making it hard for WordPress developers to understand both how to work with them, how they can be cleaned up and improved, and how to use them to their advantage rather than shun them.

To help dispel some of these issues, here’s the most straightforward, least detailed guide on what you need to do to create your own custom WordPress theme.

My ultimate goal here is to provide a very short, easy to follow, no frills guide to setting up your first custom WordPress theme. It’s very beneficial to understand the bare minimum capabilities first. It’s also much less daunting. Hopefully this helps.

Bare Minimum Custom WordPress Theme Ingredients

Very little detail, by design to keep this short and sweet:

  1. Create a new folder in your /wp-content/themes/ directory, this will be your theme.
  2. style.css contains a specific set of comments at the top to define the name of your theme (and other details). It also can contain the CSS for your theme; it will load automatically.
  3. screenshot.png is what’s shown as a preview under Appearance > Themes.
  4. page.php is the template that’s used for all the Pages you create in WordPress.
  5. front-page.php will be your homepage after you adjust your Settings > Reading to use it.
  6. post.php will be the template used for all the Posts you create.
  7. functions.php is where you add stuff that’s not part of a template.
  8. index.php lists your posts and is also the template used if no others exist.
  9. header.php is the global header that starts all templates.
  10. footer.php is the global footer that ends all templates.
  11. 404.php is what’s shown for a page not found (404 error).
  12. search.php displays search results.
  13. archive.php is a list of filtered posts used for categories, tags, dates, etc.
  14. category.php is like archive.php, but for listing posts in one category only.
  15. tag.php is the same thing but for posts by tags.

Real-world Example in GitHub Public Repo

I’ve set up a basic theme demo of this that’s publicly available on GitHub:

Full Demo on GitHub

Stylesheet Comment Format

/*
 * Theme Name: Basic WP
 * Description: Basic custom theme for WordPress that's easy to follow and learn from
 * Author: Kevinleary.net
 */

WordPress Template Hierarchy

Once you understand the pieces that make up a theme, there is a constant, noticeable pattern you can follow to work with ANY theme. It’s called the template hierarchy, and it’s how WordPress decides which file in your theme to use for every URL on your site.

WordPress Template Hierarchy Diagram

How WordPress Picks a Template

Every time a page loads, WordPress does the same three things:

  1. It works out what kind of page was requested: a single post, a page, a category, a search, and so on.
  2. It builds a list of template file names for that kind of page, ordered from most specific to most general.
  3. It looks in your theme folder for each file name in order and uses the first one it finds.

If a file doesn’t exist, WordPress skips it and tries the next one. If nothing matches, it uses index.php. That’s why index.php is the only template a theme is required to have.

Example: Category Archive

Say someone visits a category with the slug unicorns and the ID 4. WordPress looks for these files, in this order:

  1. category-unicorns.php
  2. category-4.php
  3. category.php
  4. archive.php
  5. index.php

Every other kind of page works the same way. The list starts with a file for one specific thing, moves to a file for that type of thing, and ends at index.php.

Template Order by Page Type

Page Type Templates WordPress Looks For
Front page front-page.php, then home.php (latest posts) or page.php (static page), index.php
Blog posts index home.php, index.php
Single post single-{post-type}-{slug}.php, single-{post-type}.php, single.php, singular.php, index.php
Page Custom page template, page-{slug}.php, page-{id}.php, page.php, singular.php, index.php
Category category-{slug}.php, category-{id}.php, category.php, archive.php, index.php
Tag tag-{slug}.php, tag-{id}.php, tag.php, archive.php, index.php
Custom taxonomy taxonomy-{taxonomy}-{term}.php, taxonomy-{taxonomy}.php, taxonomy.php, archive.php, index.php
Custom post type archive archive-{post_type}.php, archive.php, index.php
Author author-{nicename}.php, author-{id}.php, author.php, archive.php, index.php
Date date.php, archive.php, index.php
Search results search.php, index.php
Not found 404.php, index.php

Front Page & Blog Index

The homepage is the one place people get confused. It depends on what you choose under Settings > Reading:

  • front-page.php always wins for the front page if it exists, whether you show your latest posts or a static page.
  • home.php is for the list of blog posts. It’s used on the front page when you show your latest posts, or on the page you set as your “Posts page”.
  • page.php is used when the front page is a static page and there’s no front-page.php.

Custom Post Types

Custom post types follow the same pattern as posts. A post type named book uses single-book.php for one book and archive-book.php for the list of books, then falls back to single.php and archive.php.

Child Themes

If you use a child theme, a file in the child replaces the same file in the parent. The more specific file still wins though. A category.php in the child theme does not beat a category-unicorns.php in the parent.

Key Takeaways

  • WordPress always goes from most specific to most general, and stops at the first file it finds.
  • index.php is the fallback for everything.
  • You only need to create a more specific template when you want that page to look different.
  • To find the file behind any page in any theme, work out the page type, then walk down its list until you hit a file that exists.
  • Block themes use .html files in place of .php, but the hierarchy is the same.

The full list, including attachments, embeds and the filters you can use to change the order, is in the official WordPress template hierarchy documentation.