How can you let users to select page template for custom post type?
I’m calling register_post_type with ‘hierarchical’ => true and ‘supports’ => array(‘editor’, ‘title’, ‘page-attributes’), but template selection dropbox doesn’t appear
Thanks!
How can you let users to select page template for custom post type?
I’m calling register_post_type with ‘hierarchical’ => true and ‘supports’ => array(‘editor’, ‘title’, ‘page-attributes’), but template selection dropbox doesn’t appear
Thanks!
I’m having the same issue, have you found a solution?
Should anyone have this same issue, it’s because they are post types, not pages, hence the page template selection drop down will not show…
I used <?php if(is_single('id')){ }?> in my single.php to display the additional custom template pieces I needed.
Cheers
Hey Michael maybe this will help http://wordpress.stackexchange.com/questions/2765/adding-page-attributes-metabox-and-page-templates-to-the-posts-edit-page
See the last answer
Just change this:
add_action('add_meta_boxes','add_post_template_metabox'); function add_post_template_metabox() { add_meta_box('postparentdiv', __('Post Template'), 'post_template_meta_box', 'post', 'side', 'core'); }
To this:
add_action('add_meta_boxes','add_post_template_metabox'); function add_post_template_metabox() { add_meta_box('postparentdiv', __('Post Template'), 'post_template_meta_box', 'custom_post-type', 'side', 'core'); }
Where custom_post_type
is the slug of your post type.
Tell me the results mate
–
EDIT:Not sure if this is the best solution, Codex says “‘page-attributes’ (template and menu order, hierarchical must be true)”
Maybe you can ask in http://wpquestions.com/ or check this article http://www.twothirdsdesign.co.uk/article/2010/03/setting-templates-for-wordpress-3-0-custom-post-type/
I just found this tutorial. looks pretty good and should cover all your questions
Just add this code to your functions.php
add_action( 'add_meta_boxes', 'add_custom_page_attributes_meta_box' ); function add_custom_page_attributes_meta_box(){ global $post; if ( 'page' != $post->post_type && post_type_supports($post->post_type, 'page-attributes') ) { add_meta_box( 'custompageparentdiv', __('Template'), 'custom_page_attributes_meta_box', NULL, 'side', 'core'); } } function custom_page_attributes_meta_box($post) { $template = get_post_meta( $post->ID, '_wp_page_template', 1 ); ?> <?php $default_title = apply_filters( 'default_page_template_title', __( 'Default Template' ), 'meta-box' ); ?> <?php echo esc_html( $default_title ); ?> <?php page_template_dropdown($template); ?> <?php } add_action( 'save_post', 'save_custom_page_attributes_meta_box' ); function save_custom_page_attributes_meta_box( $post_id ) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) return; if ( ! current_user_can( 'edit_post', $post_id ) ) return; if ( ! empty( $_POST['page_template'] ) && get_post_type( $post_id ) != 'page' ) { update_post_meta( $post_id, '_wp_page_template', $_POST['page_template'] ); } }
@ kubiq: it has been 4 years so maybe he already sorted this one out
Pirenko said@ kubiq: it has been 4 years so maybe he already sorted this one out
It does not matter… I was looking for a full solution and I did not find it, so I created one and I put it here so people will find it when they will be searching…
kubiq saidJust add this code to your functions.php
Is there anything in your snippet there which should be changed? Or just past it in as-is? Because I’ve tried your code and it didn’t work to show the “Page Template” in my custom post type. I cannot find a single solution that does.
Kahil saidkubiq saidJust add this code to your functions.php
Is there anything in your snippet there which should be changed? Or just past it in as-is? Because I’ve tried your code and it didn’t work to show the “Page Template” in my custom post type. I cannot find a single solution that does.
Maybe your post_type doesn’t support ‘page-attributes’, so you have to add it - simply paste it to your functions.php:
add_post_type_support( ‘YOUR_CUSTOM_POST_TYPE’, ‘page-attributes’ );
or delete this from my code:
&& post_type_supports($post->post_type, ‘page-attributes’)
You are a legitimate king.
So glad you did post this after 4 years because I’ve just burned 4 hours trying to find a clean solution for doing just this. Worked like a charm.
Thanks, @7kubiq7!
Thanks @bbrysha But WAIT!
In new version of WordPress there is a new way how to do it
even simlier: [https://make.wordpress.org/core/2016/11/03/post-type-templates-in-4-7/]