Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Page Templates With Custom Post Types WordPress

  • SOLVED

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.

Answers (8)

2011-08-03

Zach Reed answers:

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.


Zach Reed comments:

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.


Zach Reed comments:

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?


Armand Morin comments:

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.

2011-08-03

Hai Bui answers:

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.


Armand Morin comments:

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


Hai Bui comments:

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' );


Hai Bui comments:

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' );


Hai Bui comments:

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' );

2011-08-03

Joshua Nelson answers:

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.


Armand Morin comments:

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.

2011-08-03

Clifford P answers:

How about just a stylesheet switcher? Example and code here: [[LINK href="http://www.dynamicdrive.com/dynamicindex9/stylesheetswitcher.htm"]]http://www.dynamicdrive.com/dynamicindex9/stylesheetswitcher.htm[[/LINK]]


Armand Morin comments:

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

2011-08-03

Elliott Richmond answers:

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

[[LINK href="http://wp.tutsplus.com/tutorials/widgets/using-custom-post-types-to-create-a-killer-portfolio/"]]Custom post types with template selection[[/LINK]]


Armand Morin comments:

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.


Elliott Richmond comments:

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
[[LINK href="http://wp.tutsplus.com/tutorials/widgets/using-custom-post-types-to-create-a-killer-portfolio/"]]http://wp.tutsplus.com/tutorials/widgets/using-custom-post-types-to-create-a-killer-portfolio[[/LINK]]

2011-08-03

Romel Apuya answers:

how about using a theme swithcer?


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

2011-08-03

Graham Kite answers:

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.


Graham Kite comments:

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


Graham Kite comments:

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.


Graham Kite comments:

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.


Graham Kite comments:

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.


Armand Morin comments:

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.


Graham Kite comments:

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.