The WordPress Template Hierarchy

When WordPress displays a page, it looks for a specific set of template files in your theme directory. The template files are arranged in a hierarchy, which allows WordPress to choose the appropriate file based on the type of content being displayed.

The WordPress template hierarchy starts with the most specific template and works its way up to the most general. Here is the order in which WordPress looks for template files:

  1. single-{post-type}-{slug}.php – If the page is a single post, WordPress looks for a template file named single-{post-type}-{slug}.php. For example, if you have a custom post type called “books” and a post with the slug “my-book”, WordPress would look for a file named single-books-my-book.php.
  2. single-{post-type}.php – If there isn’t a specific template for the post with the slug, WordPress looks for a file named single-{post-type}.php. This template file would be used for all posts of that post type.
  3. single.php – If there isn’t a specific template for the post type, WordPress looks for a file named single.php. This is the default template file for single posts.
  4. page-{slug}.php – If the page is a static page, WordPress looks for a template file named page-{slug}.php. For example, if you have a page with the slug “about-us”, WordPress would look for a file named page-about-us.php.
  5. page-{id}.php – If there isn’t a specific template for the page with the slug, WordPress looks for a file named page-{id}.php. This template file would be used for the page with the specific ID.
  6. page.php – If there isn’t a specific template for the page, WordPress looks for a file named page.php. This is the default template file for pages.
  7. category-{slug}.php – If the page is a category archive, WordPress looks for a template file named category-{slug}.php. For example, if you have a category called “books”, WordPress would look for a file named category-books.php.
  8. category-{id}.php – If there isn’t a specific template for the category with the slug, WordPress looks for a file named category-{id}.php. This template file would be used for the category with the specific ID.
  9. category.php – If there isn’t a specific template for the category, WordPress looks for a file named category.php. This is the default template file for category archives.
  10. archive-{post-type}.php – If the page is an archive of a specific post type, WordPress looks for a template file named archive-{post-type}.php. For example, if you have a custom post type called “books”, WordPress would look for a file named archive-books.php.
  11. archive.php – If there isn’t a specific template for the post type archive, WordPress looks for a file named archive.php. This is the default template file for post type archives.
  12. index.php – If none of the above templates exist, WordPress falls back to index.php, which is the default template file for your theme.

It’s important to note that WordPress looks for these template files in the child theme first, and if it doesn’t find them, it looks in the parent theme. If it still can’t find them, it falls back to the default template files in the WordPress core.

By understanding the WordPress template hierarchy, you can create customized templates for your theme that can be used to display different types of content. For example, you might create a custom template for a specific post type, or a custom template for a specific category. This can help you create a more tailored look and feel for your site, and can also help you organize your content more effectively.

In addition to the template files listed above, there are also several other template files that you can use to customize different parts of your site. Here are a few examples:

  1. header.php – This template file is used to display the header of your site. You can use this file to include your logo, navigation menu, and other header elements.
  2. footer.php – This template file is used to display the footer of your site. You can use this file to include copyright information, links to social media profiles, and other footer elements.
  3. sidebar.php – This template file is used to display the sidebar of your site. You can use this file to include widgets, such as recent posts, archives, and categories.
  4. search.php – This template file is used to display the search results for your site. You can use this file to customize the layout and design of your search results.

By using these template files, along with the WordPress template hierarchy, you can create a fully customized theme that meets the needs of your site and your users.

Useful Links