logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$25
Page Templates With Custom Post Types

I need to have a way for people to select a page template within a custom post type. I know this is not a standard feature with custom post types, but I've seen it done.

Any ideas?

Just to clarify, I want a drop down menu like a typical page does.

This question has been answered.

Armand Morin | 08/03/11 at 2:26pm Edit

Previous versions of this question: 08/03/11 at 2:35pm

(8) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    08/03/11
    2:33pm
    Zach Reed says:

    You could first make a custom taxonomy (like a "select category") for your custom post type and then query the post type and depending what "category" (taxonomy) they selected, it would output a different template. It's actually a lot easier than it sounds, I just did it for a client last night.

    • 08/03/11 2:34pm

      Zach Reed says:

      Your custom taxonomy add on would look like this....


      add_action( 'init', 'create_portfolio_taxonomies', 0 );
      function create_portfolio_taxonomies()
      {
      $labels = array(
      'name' => _x( 'Galleries', 'taxonomy general name' ),
      'singular_name' => _x( 'Gallery', 'taxonomy singular name' ),
      'search_items' => __( 'Search Galleries' ),
      'all_items' => __( 'All Galleries' ),
      'parent_item' => __( 'Parent Gallery' ),
      'parent_item_colon' => __( 'Parent Gallery:' ),
      'edit_item' => __( 'Edit Gallery' ),
      'update_item' => __( 'Update Gallery' ),
      'add_new_item' => __( 'Add New Gallery' ),
      'new_item_name' => __( 'New Gallery Name' ),
      'menu_name' => __( 'Gallery' ),
      );

      register_taxonomy('gallery',array('______posttypename_______'), array(
      'hierarchical' => true,
      'labels' => $labels,
      'show_ui' => true,
      'query_var' => true,
      'rewrite' => array( 'slug' => 'gallery' ),
      ));
      }


      ....that would go in your functions file to allow "categories" for your post type aka a taxonomy.

    • 08/03/11 2:36pm

      Zach Reed says:

      Then to change the output of the post type queries would be something like this...


      <?php
      $args = array(
      'gallery' => 'music',
      'post_type' => '______posttypename_______',
      'post_status' => 'publish',); query_posts( $args ); while ( have_posts() ) : the_post(); ?>

      (do something if the slug of the custom taxonomy is "gallery"

      <?php endwhile; wp_reset_query(); ?>


      Does that all make sense?

    • 08/04/11 12:38pm

      Armand Morin says:

      Zach, I just voted for your response. This looks like the way I'll be going.

      I don't know why WPQuestions changed from just allowing me to award the winner to the answer of my question. Strange.

  • avatar
    Last edited:
    08/03/11
    2:50pm
    Hai Bui says:

    Try this plugin: http://wordpress.org/extend/plugins/custom-post-template/

    It does exactly what you want, except that it enable choosing templates for all posts, not just custom post types. It works with latest WP version.

    Previous versions of this answer: 08/03/11 at 2:50pm

    • 08/03/11 8:40pm

      Armand Morin says:

      The problem is that this doesn't work on a custom post type.

    • 08/03/11 9:39pm

      Hai Bui says:

      We can modify the plugin to work with a Custom post type.
      Edit this file: wp-content/plugins/custom-post-template/custom-post-templates.php line 48

      $this->add_meta_box( 'select_post_template', __( 'Post Template', 'custom-post-templates' ), 'select_post_template', 'post', 'side', 'default' );

      please change 'post' to the custom post type name that you created. For example, your custom post type is "product" then it should be:
      $this->add_meta_box( 'select_post_template', __( 'Post Template', 'custom-post-templates' ), 'select_post_template', 'product', 'side', 'default' );

    • 08/03/11 9:42pm

      Hai Bui says:

      We can modify the plugin to work with custom post type.
      Edit this file:
      wordpress/wp-content/plugins/custom-post-template/custom-post-templates.php line 48

      $this->add_meta_box( 'select_post_template', __( 'Post Template', 'custom-post-templates' ), 'select_post_template', 'post', 'side', 'default' );


      change 'post' to the custom post type that you created. For example, if your custom post type is 'product', it should be:
      $this->add_meta_box( 'select_post_template', __( 'Post Template', 'custom-post-templates' ), 'select_post_template', 'product', 'side', 'default' );


    • 08/03/11 9:43pm

      Hai Bui says:

      We can modify the plugin to work with custom post type.
      Edit this file:
      wordpress/wp-content/plugins/custom-post-template/custom-post-templates.php line 48

      $this->add_meta_box( 'select_post_template', __( 'Post Template', 'custom-post-templates' ), 'select_post_template', 'post', 'side', 'default' );


      change 'post' to the custom post type that you created. For example, if your custom post type is 'product', it should be:
      $this->add_meta_box( 'select_post_template', __( 'Post Template', 'custom-post-templates' ), 'select_post_template', 'product', 'side', 'default' );

  • avatar
    Last edited:
    08/03/11
    2:38pm
    Joshua Nelson says:

    You can be default use single-{posttype}.php as a template for your custom post type. Then you can set up if-elseif loops in that file depending on which page it is.


    if (is_page('page1') || is_page('1')) {
    /* template for page1 */
    } elseif (is_page('page2') || is_page('2')) {
    /* template for page2 */
    } elseif (is_page('page3') || is_page('3')) {
    /* template for page3 */
    } elseif (is_page('page4') || is_page('4')) {
    /* template for page 4 */
    }


    But if you want to have a default few templates and the ability to create multiple templates, I would go the route of the custom taxonomies above or creating a custom field for each template and if statements for that, similar to above.

    • 08/03/11 8:42pm

      Armand Morin says:

      from what i see, on this solution, the problem is that let's say I have 10 custom posts under one type, i would then only be able to use the SAME template for all 10 posts.

  • avatar
    Last edited:
    08/03/11
    3:19pm
    Clifford P says:

    How about just a stylesheet switcher? Example and code here: http://www.dynamicdrive.com/dynamicindex9/stylesheetswitcher.htm

    • 08/03/11 8:43pm

      Armand Morin says:

      A theme switcher won't work. I'm hard coding this into a theme for sale.

  • avatar
    Last edited:
    08/03/11
    3:54pm
    Elliott Richmond says:

    1. register your custom_post_type probably in a separate functionality plugin
    2. be sure to add support for 'page-attributes' (menu order, hierarchical must be true to show Parent option)
    3. then create your a template file with your custom queries and layout code etc.

    The template file will then be available for selection in the edit area for your custom post type, rather than trying to code it up here try using this tutorial... it will do exactly what you need :-)

    Custom post types with template selection

    Previous versions of this answer: 08/03/11 at 3:54pm | 08/03/11 at 3:54pm

    • 08/03/11 8:50pm

      Armand Morin says:

      It appears in the post above, it looks as if you are making an archive page for post types. You still would NOT have a page template selector for the post type.

    • 08/04/11 4:19am

      Elliott Richmond says:

      Create register post type for custom posts

      $labels = array(
      'name' => _x('Books', 'post type general name'),
      'singular_name' => _x('Book', 'post type singular name'),
      'add_new' => _x('Add New', 'book'),
      'add_new_item' => __('Add New Book'),
      'edit_item' => __('Edit Book'),
      'new_item' => __('New Book'),
      'all_items' => __('All Books'),
      'view_item' => __('View Book'),
      'search_items' => __('Search Books'),
      'not_found' => __('No books found'),
      'not_found_in_trash' => __('No books found in Trash'),
      'parent_item_colon' => '',
      'menu_name' => 'Books'

      );
      $args = array(
      'labels' => $labels,
      'public' => true,
      'publicly_queryable' => true,
      'show_ui' => true,
      'show_in_menu' => true,
      'query_var' => true,
      'rewrite' => true,
      'capability_type' => 'post',
      'has_archive' => true,
      'hierarchical' => true,
      'menu_position' => null,
      'supports' => array('title','editor','author','thumbnail','excerpt','comments','page-attributes')
      );
      register_post_type('book',$args);


      make sure 'support' includes 'page-attributes' (menu order, hierarchical must be true to show Parent option)

      Then create a new page template:

      <?php
      /* Template Name: mynewtemplate */
      ?>


      within the custom post type 'book' the template 'mynewtemplate' will show up as a drop down selectable template in the Page Attributes panel down the right hand side?

      As I said before this tutorial will explain further
      http://wp.tutsplus.com/tutorials/widgets/using-custom-post-types-to-create-a-killer-portfolio

  • avatar
    Last edited:
    08/03/11
    8:26pm
    Romel Apuya says:

    how about using a theme swithcer?


    http://wordpress.org/extend/plugins/wp-theme-switcher/

  • avatar
    Last edited:
    08/03/11
    9:54pm
    Graham Kite says:

    Actually this is a standard feature of Wordpress except most themes and most people don’t know about it.
    1: create a page template with the layout and formatting you want. (this is what is missing from most themes so you never have the option of using it.)
    You can embed a whole html page into this if you want. All you need is an opening and closing php statement, or you can code a php page, with calls to whatever content and layout you want.
    MAKE SURE IT HAS A UNIQUE TEMPLATE NAME.
    2: upload this to your theme directory.
    3: once there go to insert a new page:
    You will see page attributes and then under that there will be a drop down list template.
    All you need to do is pick your new template.
    Any functionality you want here can be added to your functions.php file.
    Your question relates to pages, this is not for the posts but I would guess you could probably edit your functions to work like this also. Have never tried personally. But your question is about pages so you should be ok with this solution.

    Previous versions of this answer: 08/03/11 at 9:54pm

    • 08/03/11 9:59pm

      Graham Kite says:

      Just noticed Armand Morin is posting on this. Just buy his theme. Best thing you will do online.

    • 08/03/11 10:02pm

      Graham Kite says:

      Oops Hi Armand this is your question. Sorry didn't notice.
      Sorry I thought the question was asking about doing this on pages.
      What are you wanting to do, I thought you wanted a page template, which your theme already has.
      Are you wanting to do the same thing with your posts? Could you claify a little bit please.

    • 08/03/11 10:05pm

      Graham Kite says:

      If you are wanting to do this on custom posts, have you tried adding the code into the custom fileds area? This is anohter very powerful area that is overlooked often and you can for example post to the sidebar from there. so creating a theme options area into your custom fileds might be one solution to look at.

    • 08/03/11 10:18pm

      Graham Kite says:

      PS like where you are going with this.
      Another option you might look at.
      Tinymce advanced adds a huge lot of functionality to the mce editor.
      You can for example add a table and move it to the back or even set the backgound color, add images into ti as well as text etc.
      And using the insert a new layer feature you can drag an area anywhere you want on the screen and then position it absolutely.

      So you can literely change the look and feel of the entire post area.

      Could you look at some type of functionality like that?
      Though this is not exactly what you are asking thought it may give you a bit of food for though as this is something I would like to see in your theme.

      Hope it helps.

    • 08/04/11 12:06am

      Armand Morin says:

      Graham,

      1.) Thank you on the compliment of my theme.

      2.) I have created a custom post type and I want to add the "Template Select" drop down menu on the right in the "Page Attributes" section, just as a NORMAL page would have.

      This does require some custom coding since this is not available in the current version of wordpress.

      As far as where I'm going, I'm putting the finish on my newest version of my theme, which will use custom post types as the main way of creating sales letters.

    • 08/04/11 12:38am

      Graham Kite says:

      Yes it is possible to do that, there are several different ways.

      Here are two plugins you might like to look at.
      create custom field boxes and I think the other one is called single post template.
      I will upload a couple of images with code in them, sorry I do not have time until later to night to write it but this should head you in the right direction.

      Will look at teh code and write soem tonight if there is none posted here.

      I had trouble outsourcing a simple customisation with your theme ended up recoding the entire theme and keeping all the compatability so I have some idea of customising your theme as mine is s total custom design.

      Speaking of that I was going to add in a second sidebar on one of the template pages. Do you have a template for that in progress, I have changed the sidebar left in the theme and now want to add a template with a second sidebar.

      Will post a second image in a moment.

      I added a custom theme options to my site, It might be something to look at for your theme as well.

      Attached Image

    • 08/04/11 12:38am

      Graham Kite says:

      image 02

      Attached Image

    • 08/04/11 12:40am

      Graham Kite says:

      Hope this helps, if no code is pasted I will look at it tonight.

    • 08/04/11 12:52am

      Graham Kite says:

      I should add, Most of my customisation is css, I don't write much php, so I am not sure I would get the code right, you would need to check it carefully. :)

    • 08/04/11 1:07am

      Graham Kite says:

      One other thing, reveal id plugin shows the id of each post/page/comment etc. Once you have that ID# it is possible to target specific area of a site in the code.

      Also digi traffic multiplyer was a wso launched a couple of weeks ago.
      I takes comments from yahoo answers, adds avitars, does some nice formatting in the posts area.
      It is not a dropdown but it might be of interest to you. I bought a developers license to it, I have site that have nothing on them at present I could put up a site and let you log in the back end if you like to have a look at it?

      Hope some of this is helpful, I think this would be a great piece of functionality for some sites.
      Will be looking forward to your next upgrade.

    • 08/04/11 10:06am

      Graham Kite says:

      //*
      Found this for you on this wsbsite.

      http://www.wpbeginner.com/wp-themes/create-custom-single-post-templates-for-specific-posts-or-sections-in-wordpress/

      It does use a plugin but: 1, you could intergrate the plugin code into your functions.php or 2, make a call to an external functions,php file. Either should work.

      *//



      Custom Single Post Template for Different Posts

      This technique will let you create custom single post templates and you can select which template each post need to use as you are writing it. To accomplish this, you must download a plugin called Single Post Template and follow the directions below.

      Then you will need to create a custom file which for the examples sake we will name customsinglepost.php. You can copy your normal single.php info and change the things that you want. Or you can create a completely different design if you so desire. But one thing you must make sure that you do is to add the following code at the top of the file that you create:

      <?php
      /*
      Single Post Template: [Descriptive Template Name]
      Description: This part is optional, but helpful for describing the Post Template
      */
      ?>

      Once you are done creating the custom template for single posts, save it among your other theme files in your theme directory. Considering you already have your plugin activated when you go in your WordPress admin panel to write a new post, you will see a drop down box that gives you an option to select which template you want for that specific post.

      Single Post Template

      Note: If you do not select any template, it automatically shows the default template. You can have as many templates as you like.

      An Alternative plugin that does the same job is called Custom Post Template.

      This method above is ideal for specific posts, but if you want to have a completely different template for specific category, then the method above can be automated with a few codes in your file. This will save you time from editing each post and selecting the specific template especially if you have a lot of posts already published in that category. The automated method was shared by Justin Tadlock, one of the very famous WordPress theme developer.
      Custom Single Post Templates for Specific Category

      To use a separate single post template for each category, you will need to add the following function in your functions.php:

      /**
      * Define a constant path to our single template folder
      */
      define(SINGLE_PATH, TEMPLATEPATH . '/single');

      /**
      * Filter the single_template with our custom function
      */
      add_filter('single_template', 'my_single_template');

      /**
      * Single template function which will choose our template
      */
      function my_single_template($single) {
      global $wp_query, $post;

      /**
      * Checks for single template by category
      * Check by category slug and ID
      */
      foreach((array)get_the_category() as $cat) :

      if(file_exists(SINGLE_PATH . '/single-cat-' . $cat->slug . '.php'))
      return SINGLE_PATH . '/single-cat-' . $cat->slug . '.php';

      elseif(file_exists(SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php'))
      return SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php';

      endforeach;

      This code will tell the script to look for templates such as single-cat-uncategorized.php or single-cat-1.php within the /single folder. You must make sure that you save the files within the single folder that you will need to create in your theme directory.
      Custom Single Post Template for Specific Author

      Simply place this code in you functions.php and it will tell WordPress to look for templates such as single-author-admin.php or single-author-1.php and if it does find it in your /single folder.

      /**
      * Define a constant path to our single template folder
      */
      define(SINGLE_PATH, TEMPLATEPATH . '/single');

      /**
      * Filter the single_template with our custom function
      */
      add_filter('single_template', 'my_single_template');

      /**
      * Single template function which will choose our template
      */
      function my_single_template($single) {
      global $wp_query, $post;

      /**
      * Checks for single template by author
      * Check by user nicename and ID
      */
      $curauth = get_userdata($wp_query->post->post_author);

      if(file_exists(SINGLE_PATH . '/single-author-' . $curauth->user_nicename . '.php'))
      return SINGLE_PATH . '/single-author-' . $curauth->user_nicename . '.php';

      elseif(file_exists(SINGLE_PATH . '/single-author-' . $curauth->ID . '.php'))
      return SINGLE_PATH . '/single-author-' . $curauth->ID . '.php';

      We can’t really specify how you must create your custom single post template because that is upto you and depends on what customization you want to make, but if you get stuck feel free to ask us any question. What we must emphasize is that you must save all these custom templates in a folder named /single that must be located in your themes directory.

    • 08/04/11 10:12am

      Graham Kite says:

      Ps you have to love the name. "wpbeginners.com" I don't think I would have like to have seen that as a beginner.

  • avatar
    Last edited:
    08/04/11
    2:14am
    Christianto says:

    How about creating custom metabox "post template" and retrieve the template part with get_template_part by passing value of the post meta?

    for creating metabox


    function meta_post_template(){

    // please add other post type
    $post_types = array(
    'carpet'
    );

    // display metabox
    foreach ($post_types as $post_type){
    add_meta_box('post_template', 'Post Template', 'post_template_setup', $post_type, 'side', 'default');
    }
    // save metabox
    add_action('save_post','post_template_save');
    }

    function post_template_setup()
    {
    global $post;

    $template_type = get_post_meta($post->ID,'_post_template',TRUE);
    $template = array(
    'default',
    'portfolio',
    'design'
    );

    ?>
    <p>post template</p>
    <label class="screen-reader-text" for="post_template">post template</label>
    <select name="post_template">
    <?php foreach ($template as $option) { ?>
    <option <?php if ( $template_type == $option) { echo ' selected="selected"'; } elseif (!$template_type && $option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option>
    <?php } ?>
    </select>

    <?php
    // create a custom nonce for submit verification later
    echo '<input type="hidden" name="meta_release_noncename" value="' . wp_create_nonce(__FILE__) . '" />';
    }


    function post_template_save($post_id)
    {
    // authentication checks
    // make sure data came from our meta box
    if (!wp_verify_nonce($_POST['meta_release_noncename'],__FILE__)) return $post_id;

    // check user permissions
    if ($_POST['post_type'] == 'page') {
    if (!current_user_can('edit_page', $post_id)) return $post_id;
    } else {
    if (!current_user_can('edit_post', $post_id)) return $post_id;
    }

    if ($_POST['post_template'] != 'default'){
    update_post_meta($post_id, '_post_template', esc_attr($_POST['post_template']));
    } else {
    delete_post_meta($post_id,'_post_template');
    }

    return $post_id;
    }

    add_action('admin_init','meta_post_template');

    edit $post_types array by add your post type name, and $template array by add the template you want to show.

    then in single.php put the get_template_part code

    global $wp_query;
    $template_type = get_post_meta($wp_query->post->ID,'_post_template',TRUE);

    if(!isset($template_type) || $template_type =='default')

    // default single template to show..

    } else {

    get_template_part( 'type', $template_type );

    }


    then create file type-YOURTEMPATETYPE.php in your theme directory.

    In the example code I add "design" and "portofolio" on $template array so the file will be type-design.php and type-portfolio.php

    Previous versions of this answer: 08/04/11 at 2:14am

    • 08/04/11 1:31am

      Christianto says:

      Sorry on single.php should be

      	
      global $wp_query;
      $template_type = get_post_meta($wp_query->post->ID,'_post_template',TRUE);

      if(!isset($template_type) || $template_type =='default')

      // default single template to show..

      } else {

      get_template_part( 'type', $template_type );

      }

This question has expired.



Armand Morin voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.