Way to WordPress 4.7: Custom Page Template Functionality to All Post Types in 4.7

Custom Page Template Functionality to All Post Types in WordPress 4.7

WordPress 4.7 is set to introduce support for post type templates, an exciting new feature for theme and plugin developers. Page templates, which allow developers to specify a different layout or design for a page, are nearly as old as WordPress itself. For years developers have coveted that same functionality for other post types. In fact, a plugin that attempted to create this functionality (Custom Post Template), has more than 50,000 active installs, despite not having been updated for four years.

Post type templates follow a format similar to that of the existing custom page templates. The author can specify a template name as well as the post types where the template will be available as a dropdown in the page attributes metabox.

* Template Name: Foggy Memories
* Template Type: memory, event

The current implementation of post type templates can be used with any post type that supports page-attributes, which means that it will not work with regular posts by default. I think they would be more immediately useful for the average user who likes to tinker with templates if they worked with posts by default, but the assumption is that developers are likely the ones who will be implementing post type templates.

The ticket for the feature, which was opened four years ago, includes many potential use cases. One developer was building a project involving landing pages that were set up as custom post types. He needed to have multiple templates for the landing pages but had to create a work around since post type templates were not yet available.

Another developer wanted to make a distinction between posts with guest contributors versus those coming from regular writers, such as displaying author boxes and avatars. Since this project was for a client, an implementation involving post templates would make it easy to assign the different designs via the dropdown in the post attributes metabox.

“By opening up the page template functionality to all post types, we continue to improve the template hierarchy’s flexibility,” WordPress core committer Pascal Birchler said in his commit message. This new feature opens up many new avenues of customization for theme developers who want to offer alternative layouts and designs for posts. It also makes it easy to include multiple designs (inside a theme) for specific custom post types that a theme will support.

Source: WP Tavern


Leave a Reply

Your email address will not be published. Required fields are marked *