WordPress 4.7 Brings Custom Page Template Functionality to All Post Types

photo credit: Mari Pi
photo credit: Mari Pi

WordPress 4.7 will 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. The current implementation of post type templates can be used with any post type without having to register support for page-attributes. The metabox will be displayed if at least one template exists for a post type.

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

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.

33 Comments


  1. Wonder when we will get templates that don’t integrate with posts at all. If WP should continue to be reffered to be a CMS its theme engine needs to be able handle data that is not posts by default.

    Report


    1. I have multiple sites, and use WP (in most instances), as a CMS. Have done since 2011. In some scenarios, the intergrated posts aspect is a frustration. I always find a work around, (no I don’t hack the core).

      However it’s a test I’d rather not sit.

      Hence, agree with Andreas Nurbo. Would be a huge-step-forward when WP can handle data that “is not posts by default.”

      Report


      1. Can’t you just use `$wpdb` and it’s database manipulation classes to insert, replace, or otherwise manipulate data in a custom table and display them with WP aided PHP?

        Seems like the functionality is already there really, and if you want a simpler way, the middle-of-the-road approach of custom post types (really a misnomer for custom data types) with custom fields and attributes is a decent alternative.

        Report


      2. Its not. WordPress makes calls and tries to populate post objects by default. Disabling the WP_Query is a hassle. Like disabling 3-4 hooks. If you want to display custom content in a standard theme you need to override the default post functionality and stuff your data in a fake post object.

        Report


  2. Based on what this feature offers and what Post Formats does, what is the difference between the two? Does this replace Post Formats or are we going to have two different ways to do the same thing?

    Report


    1. No, post formats are about being theme neutral. They indicate what the theme should attempt to do with the content but do not force it. Templates OTOH are theme specific.

      It is just another variant that can be used for doing post formats, you could have styled them differently by marking then with a specific category/tag, use a custom field value or create a special CPT. This is just another alternative and probably the least efficient in the specific case of post formats.

      Report


    2. Post formats are for posts (post type post). Post do not have custom, user selectable templates (in core). The number of different templates a theme may provide for post formats are limited to the core formats.

      This feature enables what pages have today. Custom post types now have all the features pages have. A theme may provide any number of such templates, for whatever purpose.

      Report


    3. Post Formats describe what type of content a post is. As Mark K mentions, that is theme-independent. A template is an arbitrary theme-specific choice for how content looks. I think they’re pretty unrelated.

      Report


  3. The template feature as it is in pages by itself is bad UX, for example no inheritance for subpages. Except for leveling the field for all content type I fail to see any reason for excitement.

    Report


    1. I personally hate when templates do any inheritance. Umbraco does this and it’s all just a huge nightmare.

      Report


      1. @Pat Hartl, it is part of why the UX sucks. A user might expect to have an inheritance, after all in many other things a child inherits properties of the father, but not in wordpress, and as you point out when you decide to handle it differently it is surprising in a bad way.

        But without inheritance it means that if I have 200 pages of documentation I need to change the template for all of them instead of just the parent.

        Report


      2. Yes and no. One could easily implement such a feature as a plugin; the template functionality is (currently) implemented as a post meta, anyway.

        Something as simple as an additional meta field, eg. _wp_template_parent, or if you reference a parent page, _wp_template_parent_id.

        cu, w0lf.

        Report


  4. I think WordPress is in danger of “jumping the shark”, becoming too complicated and loaded with too many features. Google has mostly forgotten all about Blogger, but it may become a better alternative for a lot of people who just want a simple business site. WordPress seems to be something for people who want to spend time and money on a fancy site with a lot of features. How many businesses really need all of that? Not many.

    As someone who has kept sites for many years and used WordPress most of the time, I’m not planning to use a lot of customized posts. I don’t need them. I want to focus on content, not spend a lot of time on formatting.

    I will add that if people are building a site to function as a web directory, job board, or any of a hundred other things – WordPress isn’t an essential element. It may even be a hindrance. WordPress is still a customized blog at heart.

    Report


    1. WP became a general purpose CMS a long time ago. It’s used by newspapers for God’s sake!

      Report


      1. WP became a general purpose CMS a long time ago.

        It’s a pity so many people still don’t seem to grasp this, even some core devs!

        Report


      2. How far can you get without plugins? Plugins add function to WordPress but they also make it less reliable. A general purpose CMS is something a general user could turn into any kind of content. At least that is how I see it. I like WP. I test out a lot of plugins when I get an idea I’d like to run on my sites. I spend more time trying to get WordPress to work with plugins than I do just using other software in the first place. I’m not building brochure sites. I’ve been using less images on WordPress and running them with other software, outside of WordPress.

        I did not post to slam WP. I just think the software is getting more complicated and becoming limited to web developers rather than ordinary users. Possibly this is intentional. For me it’s not working so well. I need time to create my art, promote my art and write and do all the other things I need to do. I don’t make money from my sites – I spend money on them and time on maintaining them. I’d like to limit that so having a site is sustainable for me. Hiring a web developer to help me figure out WordPress’s latest features is not sustainable for me. At some point the ordinary user is going to find Blogger, or something else, a better, sustainable solution.

        Report


    2. If what you want is a 5-page brochure site where you don’t update the content more than once a year, WordPress is a bad choice, though I don’t think Blogger would be better. You’re probably better off looking for an HTML site-building tool (e.g. Blocs for Mac) or using something like Wix (though I hesitate to mention them just at present given their bad behavior) or SquareSpace. If you just want a blog, you could use WordPress.com and never encounter a custom post type. But for the kind of sites many of us build, which use many types of content, this is good news. You could use it to create different layouts within a single portfolio post type, for instance, when people do different kinds of work.

      Report


      1. I have been looking at HTML Editors and other software for my ideas. I know WordPress wants to be more than blogware but I find it still has limits for people who want to build a site without learning a lot of software. WordPress relies on plugins, quite heavily. This makes it adaptable to some degree but it limits it too.

        Thank you for your comment on my site Jim Wilson at virtualpromote.com I thought I’d share it here since I won’t be posting it myself:

        Every time you post on WPTavern you prove just how stupid you are.

        https://wptavern.com/wordpress-4-7-brings-custom-page-template-functionality-to-all-post-types

        Please. Just. Stop.

        Other people are embarrassed for you.

        On the web since 1996 is NOT a badge of honor when your skills are still in 1996 like yours clearly are. You are a waste of space and quite frankly, a sad excuse for a human being.

        Note: I consider WPTavern a forum/ blog for people using WordPress. I don’t think of it as an elite club for WordPress developers. I’m posting for all the sad excuses for human beings, Jim. We are out here and we think you’re the ignorant one.

        Report


  5. Although I can see this giving some flexibility in how a “single” post is displayed, for example, a portfolio item, I’m wondering how this will affect the user if they later change themes.

    What I would LOVE to see the core team create is the ability to choose blog layouts by way of “page templates” or rather “blog templates”. Give the ability to have various blog layouts (incl. archives such as categories) from a template choice from the customizer. Just like choosing a page template for a page, this would be done the same way for blog layouts, like a grid layout, masonry, right sidebar, left, full width…etc. Right now themes have to create multiple files and conditionals, if/else, etc etc to achieve that.

    Report


    1. Agree 100% – Then all we need is an easy way to create new archive URLs :)

      Report


    2. I think things like blog layouts have to be part of the theme, and indeed many themes do provide options for different blog layouts. And there are some plugins to help you set that kind of thing up, too. But if you try to separate it from the theme, you have a much greater likelihood of ending up with something that looks pasted on.

      However, as long as your CPTs are created in a functionality plugin, that data will be perfectly safe if you change themes. (The data would actually be perfectly safe in your database regardless, but you wouldn’t see it unless the new theme had the code to display it.) And you might choose to include CPT templates within said functionality plugin, though this is a little trickier than just putting them into the theme folder.

      Report


  6. By using this, we will save time and can gather all the snippet into one template, if else will help much.

    But that’s only in the case of simple template and layout. When a templates is too different from the layout or custom fields (using the advanced custom field, maybe), it would be hard to customize and making everything look exactly as design (or HTML template).

    So I think it’s just the first step only of bringing multiple template tag into WordPress, what I’m actually thinking is, WordPress in future will have the ability to create custom fields, view, and relation between fields, views, types (CPT) in the right your admin, it would look like Odoo (openERP) is doing

    Report


  7. Glad to be able to use this really useful feature. Can immediately see how this would have simplified the code on my last project, would have needed far fewer conditional tags.

    Report

Comments are closed.