839 posts
  • Power Elite Author
  • Winner
  • Top Monthly Author
  • Trendsetter
+10 more
wpbakery
says

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!

2 posts
  • 6 Years of Membership
  • Collector Level 1
pauliewalnuts
says

I’m having the same issue, have you found a solution?

2 posts
  • 6 Years of Membership
  • Collector Level 1
pauliewalnuts
says

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

4483 posts ThemeForest Senior Reviewer
  • Affiliate Level 1
  • Author Level 6
  • Collector Level 3
  • Envato Team
+14 more
Ivor
Envato team
says

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 :grin:

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/

751 posts
  • 6 Years of Membership
  • Affiliate Level 1
  • Collector Level 2
  • Exclusive Author
+1 more
CyberShot
says

I just found this tutorial. looks pretty good and should cover all your questions

http://www.vooshthemes.com/blog/wordpress-tip/create-a-professional-portfolio-using-wordpress-3-0-custom-post-types/

3 posts
  • 2 Years of Membership
  • Collector Level 1
  • Slovakia
kubiq
says
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 ); ?>
    <select name="page_template" id="page_template">
        <?php $default_title = apply_filters( 'default_page_template_title',  __( 'Default Template' ), 'meta-box' ); ?>
        <option value="default"><?php echo esc_html( $default_title ); ?></option>
        <?php page_template_dropdown($template); ?>
    </select><?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'] );
    }
}
698 posts Fuel Your Web
  • Trendsetter
  • Weekly Top Seller
  • Elite Author
  • Winner
+6 more
Pirenko
says

@ kubiq: it has been 4 years so maybe he already sorted this one out :D

3 posts
  • 2 Years of Membership
  • Collector Level 1
  • Slovakia
kubiq
says

@ kubiq: it has been 4 years so maybe he already sorted this one out :D

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…

1 post
  • 2 Years of Membership
  • Collector Level 2
  • Exclusive Author
  • United States
Kahil
says

Just 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.

3 posts
  • 2 Years of Membership
  • Collector Level 1
  • Slovakia
kubiq
says


Just 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')
by
by
by
by
by
by