WordPress Select Template in Custom Post Type

I had what seems to be a common problem applying a custom template to a custom post type in WordPress.

First, I up the post type with custom fields using Custom Field Suite and Custom Post Type UI.  I initially had the a custom template set up and working properly, pulling in the corresponding custom fields that I created for a standard “page” post type.   However, when I attempted went to select the template for use with a custom post type, the drop down to select the custom template didn’t show up in the attributes tab like it did on the pages. Come to find out that WordPress natively doesn’t allow for templates in posts. Ugh!

After looking around and trying several solutions I found the Custom Post Template plugin.

To Install :

  • Download the plugin, it will arrive as a zip file
  • Unzip it
  • Upload custom-post-template directory to your WordPress Plugin directory
  • Go to the plugin management page and enable the plugin
  • Upload your post template files (see below), and choose them through the new menu

Be sure to change the template name in the header of your template:

<?php /* Template Name Posts: Inforest 1 */ ?>

If using a Custom Post Type add the code below to your Functions file and change $post_types[] = ‘movie’; to the name of the post type you are using:

/**
 * Hooks the WP cpt_post_types filter 
 *
 * @param array $post_types An array of post type names that the templates be used by
 * @return array The array of post type names that the templates be used by
 **/
function my_cpt_post_types( $post_types ) {
    $post_types[] = 'movie';
    $post_types[] = 'actor';
    return $post_types;
}
add_filter( 'cpt_post_types', 'my_cpt_post_types' );