Custom templates for posts like for pages: page-{id} vs single-{id}

A) Single Post Templates: single-{post-type}-{post-name}.php

It’s informative to check out the get_single_template() function.

There we find the following template possibilities:

"single-{$object->post_type}-{$object->post_name}.php"
"single-{$object->post_type}.php"
"single.php"

In WordPress 4.7 this part will be added:

"single-{$object->post_type}-{$name_decoded}.php"    

So it looks like you forgot the {$object->post_type} part in the template’s name.

Example:

The single template name for the post with the slug hello-world:

single-post-hello-world.php

B) Custom Single Post Templates

Another feature in WordPress 4.7 will be the support for custom single post templates.

Check out ticket #18375 for more info.

Example:

I just tested this new feature, by creating a template file, e.g. tpl-wpse-test.php, in the current theme directory:

<?php
/**
 * Template Name:      WPSE Template Test
 * Template Post Type: post
 */
 get_header();
 ?><div id="main"> WPSE Template Test - Success! ;-) </div><?php
 get_footer();

Then the following meta box shows up automatically in the backend for the corresponding post types.

Metabox

There’s a multi post type support as well, e.g.:

 * Template Post Type: posttype1, posttype2, posttype3

But there’s no need to use add_theme_support(), like we use to get the featured image meta box.

Leave a Comment