WordPress Theme Post Template Hierarchy Explained
Filed under WordPress Theme Development
WordPress utilizes a template hierarchy for themes. What does that mean?
Think of it as a series of “if, else” statements.
When a page, post, or attachment url is called, WordPress looks for matching templates – from specific (ex: a template for a specific category) to generic (the index.php template)
Post Template Hierarchy
mycustomtemplate.php(selected from Post Attributes)
Criteria #1: The WordPress core will first check to see if you selected a custom template from the dropdown under the “Post Attributes” section.
For example, you could create a unique template called
fancypost.php. Just include the following HTML comments at the top of your template file:
<?php /* * Template Name: Fancy Post * Template Post Type: post */
The custom template will now show up on all standard blog posts under the Post Attributes like so:
Criteria #2: If you have not selected a custom template, it will use the
single-post.php template. As the name suggests, only single posts will use this template.
Criteria #3: If
single-post.php doesn’t exist, it will check for the
single.php template file.
What’s the difference between
single.php? Both attachments and custom posts use
single.php within their hierarchy structures as well.
Criteria #4: After that, it will check for
singular.php (which is a catch-all for pages, posts and attachments).
Criteria #5: Finally it will default to using
Additional WordPress Post Template Hierarchy Resources
- A visualization of the entire WordPress theme template hierarchy.
- How to register a custom post type in WordPress.