Post Type Templates in 4.7

This caught my attention: in WordPress 4.7, all post types will finally be able to have the page templates feature that was previously only available on (the) pages (post type). You’ll just have to add an extra line to the top of the relevant template file that specifies the post types you want it to display. Pascal Birchler explains:

In addition to the Template Name file header, the post types supported by a template can be specified using Template Post Type: post, foo, bar. Here’s an example:

<?php
/*
Template Name: Full-width layout
Template Post Type: post, page, product
*/
 
// … your code here

Personally, I’ve never really loved the page template feature in WordPress. But like many other features like it, it’s so darn useful sometimes that you really can’t fight it for long. It’s just so clearly an improvement over any alternative that has ever crossed my mind that I’ve no doubt that this feature being available for all kinds of content in a WordPress site will work out as a net win. When you don’t like it, you can simply not use it. When you need it, it’ll be there. That’s ideal.


4 Responses

Comments

  • mohamed elhussini says:

    this code write in page templete or at custum post code
    can you more explain with examples

    • Fred Meyer Fred Meyer says:

      Hi Mohamed,

      This goes at the top of any post template.

      In David’s example, if you make a PHP file called name-me-anything.php and put his code at the top, you’ll then have a “Full-width layout” template for your Posts, Pages, and Products.

      Those pages on your site will look like whatever you write inside the contents of name-me-anything.php.

      Does that help?

Add a Comment

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