For further reading
For more information on the WordPress theme hierarchy, please refer to https://developer.wordpress.org/themes/basics/template-hierarchy/
When you are designing your blog and creating your required files, there are certain files that you need to bear in mind. Each can impact and take precedence over another.
index.php
Your theme’s index.php file will be the fallback file for all of your posts and archives. In Project Zero, it controls the primary blog archive, but you can ostensibly control your entire theme with just an index.php file.
archive.php
Your archive.php file is used for any archive page for any post type that does not have a specific archive. To create a post-type specific archive, you can use archive-{post_type}.php to rename your archive file. For example, archive-portfolio.php is used for a ‘portfolio’ post type, and archive-post.php is used for blog posts (’post’ post type).
In Project Zero, it controls the secondary blog archives. These are archive pages for the following archives:
For information
Included in this list are the respective theme files that would supersede archive.php if they were included.
- Author archive – uses
author.php. Can also useauthor-{id}.phporauthor-{nicename}.phpfor more specificity. - Category archive – uses
category.php. Can also usecategory-{id}.phporcategory-{slug}.phpfor more specificity. - Tag archive – uses
tag.php. Can also usetag-{id}.phportag-{slug}.phpfor more specificity. - Taxonomy archive – uses
taxonomy.php. This refers to custom taxonomies rather than the post category and tag taxonomies. Can also usetaxonomy-taxonomy.phportaxonomy-{taxonomy}-{term}.phpfor more specificity. - Date archive – uses
date.php.
single.php
Your single.php file is used for any single post page for any post type. To create a post-type specific single page, you can use single-{post_type}.php, in the same pattern as your archive page. For example, single-portfolio.php is used for a ‘portfolio’ post type, and single-post.php is used for blog posts (’post’ post type). You can also use single-{post_type}-{slug}.php for greater specificity.
Bear in mind ...
There is no single-slug.php file. To apply a template to a blog post based on the post slug, you need to use single-post-{slug}.php.
Just so you know
Your single.php template can be overridden by a custom post template. For more information, read the Custom Post / Page Templates page.
page.php
page.php file is used for any single static page. It is not used for custom post types – this is single.php or single-{post_type}.php. You can use page-{id}.php or page-{slug}.php for greater specificity. Bear in mind ...
There is no single-page.php file. Since page.php is specifically to render static pages, it will be used for all ‘page’ post types.
Just so you know
Your page.php template can be overridden by a custom post template. For more information, read the Custom Post / Page Templates page.
attachment.php
Your attachment.php file is used for any media files that you link to. You can link directly to the media file or the attachment page, depending on what you prefer. You can use single-attachment.php as an alternative.
For more specificity, you can use single-attachment-{slug}.php. You can also use {mine_type}.php to create a template for specific file types. For example, image.php for all images, video.php for all video files, and pdf.php for all PDF files.
singular.php
All single posts fall back to singular.php when their respective single page is not available. To demonstrate its use, let’s assume your site was trying to load mysite.tld/portfolio/myproject/.
- First, it would look for
single-portfolio-myproject.php, since the post type would be ‘portfolio’ and the post slug would be ‘myproject’. - If that is not available, it will fallback to
single-portfolio.php, as the post type would be ‘portfolio’. - If that is not available, it will fallback to
single.php. - If that is not available, it will fallback to
singular.php. - If that is not available, it will ultimately fallback to
index.php.