Solving The WordPress Template Hierarchy Puzzle: Posts and Pages

Josh Pollock - May 01, 2014

http://joshpress.net/blog/category/wordpress/tutorials/solving-the-wordpress-template-hierarchy-puzzle/http://joshpress.net/blog/category/wordpress/tutorials/solving-the-wordpress-template-hierarchy-puzzle/

So far in my series on the WordPress template hierarchy I have covered a lot of ground, but so far I have avoided single posts and pages, which I will be covering today. By default, there is no archive view for pages, and post archive is displayed in the blog index. Which templates control the blog index was the subject of yesterday’s post. This article will focus on which templates are used to display single post and page views.

It’s important to keep in mind that posts and pages are two post types. WordPress has three built-in post types. The third one is attachments. WordPress also allows you to add additional custom post types. I covered the template hierarchy for custom post types in an earlier post. The fact that you can have a post in the page post type or a post in a custom post type is confusing, I know. It’s just something you have to get if you want to understand WordPress templating.

Single Posts

In my post on the template hierarchy for custom post types I showed the hierarchy for custom post type single post view, which was:

single-{post-type-name}.php -> single.php -> index.php

Since posts are a post type, called post, the hierarchy for posts follows this pattern and looks like this:

single-post.php -> single.php -> index.php

This means that “single.php” is the fallback template for all post types, including post and custom post types–well except pages which we will get to in a second. If you are not using any custom post types, you can use “single.php” for your single post views. Just keep in mind it will also be used for attachment pages.

The situation where the “single-post.php” template comes in handy is when you have multiple custom post types that are being displayed using “single.php” and you want to make changes to the view for posts only.

Single Pages

The template hierarchy for pages does not follow the pattern for other post types. Pages are just special I guess. Their hierarchy works in a very similar  way to how categories and tags work, with the ability to create templates for individual pages by slug or ID.

The template hierarchy, if a custom template is not being used is:

page-{page-slug}.php -> page-{page-id}.php -> page.php -> index.php

Just like with categories and tags, templates named for slugs take precedence over templates name for IDs. So if you have a page whose slug is “lightsaber” and has the ID of 7. “page-lightsaber.php” would be used, even if “page-7.php” existed in the current theme as well. I would strongly urge you to avoid using templates name for page slugs, as page slugs can change, while IDs should never change.

Of course, pages can also be assigned custom page templates in the post editor for the page. Custom templates take precedence over any of templates listed above. This is why “page-{page-slug}.php” and “page-{page-id}.php” templates are not commonly used. Using custom page templates are more flexible and easier to work with.

Conditional Tags…

Normally I end these posts with a discussion of how to use conditional tags to avoid creating separate templates. But I want to keep this post concise, and the difference between is_single() and is_singular() is worthy of it’s own post, especially when you throw in is_home(), is_front_page() and is_page(). So, I will save that for next week.