override-theme-templates

How to Override Theme Templates in WordPress

Last Updated on April 29, 2022 by WP Knowledge Hub

WordPress themes are created with a set of templates. In order to function, a WordPress theme needs (at the very least) an index.php template and a functions.php template. Of course, real themes come equipped with much more than that. Most themes come with these templates (or more):

  • 404.php
  • archive.php
  • comments.php
  • footer.php
  • functions.php
  • header.php
  • index.php
  • page.php
  • search.php
  • searchform.php
  • single.php
  • and a stylesheet, style.css

These templates, or parts, make up the theme which establishes your website’s basic design. Templates can be overridden to create a new layout, or to add additional content directly to the template. In order to understand how overriding templates work, you need to understand the WordPress Template Hierarchy.

Here is a basic example:

  • A user visits a category archive on your website
  • WordPress looks for a custom template in the theme for that category, which would look something like archive-yourcustomcategory.php
  • If there is no specific custom template defined it will use a general archive.php template
  • If there is no archive.php template, it will use index.php, the most basic template with a WordPress loop

How Do I Override WordPress Templates?

A basic post template is single.php, a basic page template is page.php, and a basic category template is archive.php.

Since WordPress looks for a custom template first, you have to create a custom template with this specific naming convention:

  • single-yourcustomname.php
  • page-yourcustomname.php
  • archive-yourcustomname.php

There can also be templates defined by the slug of your page or post. If you have a page titled Checkout, whose slug is checkout, the specific template would be page-checkout.php.