Results 1 to 10 of 10

Thread: Custom post types and different templates

  1. #1
    andrea_r's Avatar
    andrea_r is offline WordPress Rockstar
    Join Date
    Jan 2009
    Location
    Eastern Canada
    Posts
    1,325

    Default Custom post types and different templates

    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?

  2. #2
    Utkarsh is offline Hello World
    Join Date
    Nov 2009
    Posts
    73

    Default

    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.

  3. #3
    andrea_r's Avatar
    andrea_r is offline WordPress Rockstar
    Join Date
    Jan 2009
    Location
    Eastern Canada
    Posts
    1,325

    Default

    I wonder if they work like I need them to work...

  4. #4
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,797

    Default

    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.

  5. #5
    andrea_r's Avatar
    andrea_r is offline WordPress Rockstar
    Join Date
    Jan 2009
    Location
    Eastern Canada
    Posts
    1,325

    Default


    Just make a custom post template, then use require( 'page-bla.php' ); to load the page template inside it.
    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.

    the custom post type is perfect for the client's needs on the user side, except for this one thing.

  6. #6
    greenshady's Avatar
    greenshady is offline Here For The Peanuts
    Join Date
    Jan 2009
    Posts
    170

    Default

    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.

  7. #7
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,797

    Default

    Quote Originally Posted by greenshady View Post
    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.

    Quote Originally Posted by andrea_r View Post
    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.

    the custom post type is perfect for the client's needs on the user side, except for this one thing.
    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.

  8. #8
    mfields's Avatar
    mfields is offline Here For The Peanuts
    Join Date
    Nov 2009
    Location
    Portland, OR
    Posts
    168

    Default

    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?
    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 :)

    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 ( 
    != countget_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
        
    }
    }
    ?>

  9. #9
    andrea_r's Avatar
    andrea_r is offline WordPress Rockstar
    Join Date
    Jan 2009
    Location
    Eastern Canada
    Posts
    1,325

    Default

    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?)

  10. #10
    itsananderson's Avatar
    itsananderson is offline Big Tipper
    Join Date
    Jan 2009
    Location
    Terre Haute, IN
    Posts
    354

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •