Adding custom Post templates in WordPress Themes was always something that WP users wanted and Theme developers came up with various solutions for it. The problem is that Pages had the option to use custom templates like forever by adding the
Template Name: tag but there was no standard way for doing the same for Posts. Until now!
WordPress 4.7 introduced the
Template Name tag for Posts and even custom post types! Yay!
Here’s how it looks in the Post editor page:
Of course all our themes have already built in this feature, so now you can select a Post Template the same way you can select a Page Template.
To add a Post Template to your Theme, you need to add a single line after the
Template Name tag:
Template Post Type: post, page– for Posts and Pages
Template Post Type: post, page, books– for Posts, Pages and a custom post type (books in this example)
Note: The styling must be made accordingly of course.
DO NOT name your file with
page- prefix (ex:
page-full-width.php) as the
page- prefix will be reserved for single page templates in the future in WordPress! We changes all template file naming to
template- prefix, but you can chose whatever it fits best for your theme, however, try to keep in mind also the WordPress standards.
Since we are using the same Template file for both Posts and Pages, a condition can be added to the template file to pull in the correct template part. See how we did it in the in our themes: