Yeah, yeah, I know... posts are post and Pages have page templates...
But what if custom post types had the ability to pick a page template? Possible? Way out there in left field?
thoughts?
Yeah, yeah, I know... posts are post and Pages have page templates...
But what if custom post types had the ability to pick a page template? Possible? Way out there in left field?
thoughts?
Hybrid theme already implements templates for Custom Post Types.
If you’ve used WordPress for any period of time, you’re probably familiar with the concept of page templates. This is especially so if you’ve taken advantage of Hybrid’s numerous page templates.
Introduced in version 0.7 of the Hybrid theme is the concept of custom post templates. These work in the same way as page templates.
Just make a custom post template, then use require( 'page-bla.php' ); to load the page template inside it.
I don't think Hybrid can load page template for custom post-types, I'm pretty sure it has entirely separate templates for it ... I could be wrong though as I haven't checked.
What if I want the user to be able to pick a page template from a drop-down. ;) the idea is, the content will be the same, but the user will have a choice of template sot use just for that page.
Just make a custom post template, then use require( 'page-bla.php' ); to load the page template inside it.
the custom post type is perfect for the client's needs on the user side, except for this one thing.
Hybrid definitely allows custom templates for any post type. Just create the custom template and the drop-down select automatically appears on the post editing screen. It's had this feature since the end of last year.
Yes, but that's not what Andrea's asking about here. She's asking to use page templates with her custom post template, not a regular custom post template.
I assume you would need to create separate custom post templates to load each page template. Maybe there's a way to do that without manually creating them, but I've never heard of it.
Totally possible. The following code should satisfy proof-of-concept. You will most likely want to add in some filtering, security and permission checks where appropriate though. Please let me know what you think :)Yeah, yeah, I know... posts are post and Pages have page templates...
But what if custom post types had the ability to pick a page template? Possible? Way out there in left field?
thoughts?
PHP Code:<?php
/**
* Post_type
*/
define( 'MY_THEME_POST_TYPE', 'resource' );
/**
* Load the page template for any post object
* having the appropriate meta key set.
*/
add_action( 'template_redirect', 'mytheme_template_redirect' );
function mytheme_template_redirect() {
global $wp_query;
$id = (int) $wp_query->get_queried_object_id();
$template = get_post_meta( $id, '_wp_page_template', true );
if ( $template && 'default' !== $template ) {
$file = STYLESHEETPATH . '/' . $template;
if( is_file( $file ) ) {
require_once $file;
exit;
}
}
}
/**
* Process the Meta Box
* @todo Permissions check.
* @todo Filter input.
* @todo Nonces.
*/
add_action( 'save_post', 'mytheme_process_resource_template' );
function mytheme_process_resource_template() {
global $post;
/* Sanitize $_POST array. */
$clean_id = ( isset( $_POST['ID'] ) ) ? intval( $_POST['ID'] ) : 0;
if ( !empty( $_POST['page_template'] ) && MY_THEME_POST_TYPE == $post->post_type ) {
$page_templates = get_page_templates();
if ( 'default' != $page_template && !in_array( $_POST['page_template'], $page_templates ) ) {
if ( $wp_error )
return new WP_Error('invalid_page_template', __('The page template is invalid.'));
else
return 0;
}
update_post_meta( $clean_id, '_wp_page_template', $_POST['page_template'] );
}
}
/**
* Registers the Meta Box
* @uses mytheme_page_attributes_meta_box()
*/
add_action( 'admin_init', 'mytheme_register_meta_boxes', 10 );
function mytheme_register_meta_boxes() {
add_meta_box(
'mytheme_post_type_template',
'Template',
'mytheme_page_attributes_meta_box',
MY_THEME_POST_TYPE,
'side',
'low'
);
}
/**
* Creates the Meta Box
*/
function mytheme_page_attributes_meta_box() {
global $post;
$post_type_object = get_post_type_object($post->post_type);
if ( 0 != count( get_page_templates() ) ) {
$template = get_post_meta( $post->ID, '_wp_page_template', true );
?>
<p><strong><?php _e('Template') ?></strong></p>
<label class="screen-reader-text" for="page_template"><?php _e('Page Template') ?></label><select name="page_template" id="page_template">
<option value='default'><?php _e('Default Template'); ?></option>
<?php page_template_dropdown( $template ); ?>
</select>
<?php
}
}
?>
mfields - i think that's something like the conclusion we came to last night. :)
I love handing you guys tough theory questions. :D
So, the use-case here is the user is going in, seeing the custom post type, stuff in their content, and now they get to pick drastically different templates they can change at any time. Neat-o.
When we get something up, I'll try and remember to post back.
(has anyone done this before anywhere?)
It seems to me that having the ability to specify in the custom page template what kind of post type(s) are supported would be pretty simple. It seems like something that should be built into core, but since 3.1 won't be here anytime soon, perhaps a code snippet could be developed that could be placed in functions.php for themes that define "page" templates for other post types.