Custom Post Type Custom Template ?

Been researching how to create custom template for custom post type in a plugin. This is a note about what i found:

1. can add filter for ‘template_include’ hook, and add a meta box for switching your own custom template (create your own dropdown menu)

For example:

add_filter('template_include', 'template_include_function', 10, 1);

function template_include_function($template) {
	global $post;
	if ($post->post_type == 'YOUR_CUSTOM_POST_TYPE') {
                $postmeta = get_post_meta(...);  
		...
		$template =  plugin_dir_path(__FILE__) . YOUR_TEMPLATE_FILE;
                ...
	}
	return $template;
}

function template_box() {
	add_meta_box(
		'template_box',
		__('...','...'),
		'template_content',
		'YOUR TYPE',
		'side',
		'core'
	);	
	
}

function template_content($post) {
	$template = get_post_meta(.. 
?>
<label ...>_e(...);</label>
<select ...">
<option ...>Option 1</option>
<option ...>Option 2</option>
</select>

<?php
}

add_action('add_meta_boxes', 'template_box');
&#91;/php&#93;

then add your own meta box to display this template list.  Use action hook 'save_post' to update post meta data for your template selection

2.  I have tried to add Page Attributes capability, but it does not show the custom template in the theme.  I looked at the code for function page_attributes_meta_box, and found the a condition that will check if post type is 'page', then will show the template.  Therefore, it is not gonna call the page_template_dropdown(), and no page template shows up in the admin page.

3. a filter 'page_attributes_dropdown_pages_args' is apply if the custom post type supports hierarchical, so one thing can try is add filter for   'page_attributes_dropdown_pages_args' and add drop down menu that point to your template directory.  In your filter function, you can try

&#91;php&#93;
function YOURFUNCTION($dropdown_args, $post) {
if ($post->post_type == 'YOUR CUSTOM POST TYPE') {
...
<select ...>
<?php page_template_dropdown(false); ?>
</select>
...
}

return $dropdown_args; // just return the same dropdown args. don't need to modify
}

This basically will create a drop down menu for all template in your Theme directory (not your plugin). Note that this drop down menu will appear above the Parent drop down menu(for hierarchical setting). This is useful if you have custom post type in your theme and template file within your theme directory.

4. So instead of calling page_template_dropdown(), it is better to add your own template here if you do not want to add extra meta box.

function YOURFUNCTION($dropdown_args, $post) {
if ($post->post_type == 'YOUR CUSTOM POST TYPE') {
...
<select ...>
<option ...> Option 1 </option>
<option ...> Option 2 </option>
</select>
...
}

return $dropdown_args; // just return the same dropdown args. don't need to modify
}

add_filter('page_attributes_dropdown_pages_args', YOURFUNCTION, 10, 2);

Ok, it is not done yet. You add options here, then you also need to add action to ‘save_post’ hook to update the the post meta. the post meta will be used in the filter hook ‘template_include’. (look at step 1)

5. Right now I am studying WP_Theme class get_page_templates() to see how it parses template in the theme directory.

Note: I think create your own meta box for templates for your own custom post type is more appropriated…

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

Leave a Reply