WordPress at a glance
Home WordPress Codex

Page Template for any Post Type in WP 4.7

Support of templates for pages (page post type) has been unchanged for 12 years. And it helped developers to conveniently create templates for pages, but it was always limited to the page post type. However, with WordPress version 4.7 the situation has changed and you can now create templates for any post type.

Templates for 'page' Post type

I already wrote about how to create templates for page post type and that article has not only this way, and two more. This variant goes there the first. Let me remind you: to create a page template, you need to create a file with any name in your WordPress theme, for example, `my-template.php ' and specify in it the following php comment:

Template Name: My page template

<!-- Here is html/php the template code -->

A template file can be placed even in a subfolder of the theme, WP will still find it. For example, you can create a folder with name 'templates' in your theme, and put there all created template files.

After that, on the page editing page in the admin in the section 'Page Attributes', you will be able to select this template.

Templates for any Post type

Since WordPress version 4.7 the functionality of page templates has expanded. And now you can create templates for a post of any type.

To specify that the template file you are creating is supported by a specific post type, you must specify the name of the post type in the title: Template Post Type:. Multiple post types can be specified with a comma.

For example, let's create a template that supports three types of post: post, page, product. To do this add a php comment at the beginning of the file:

Template Name: My page template
Template Post Type: post, page, product

// … other template code

Now on the admin edit page of the post types: post, page, product, we can select our template «My page template».

Notes for Developers

  • If at least one template exists for a post type, the Metabox Post Attributes appears automatically on the edit post page. It is not necessary to enable support of 'page-attributes' when registering post type.

  • The Metabox title Post Attributes can be changed in parameter label > attributes during registration of the post type.
No comments
    Hello, !     Log In . Register