Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

can I assign a template file to custom post types?

I have created a custom post type called items, and I would like to assign templates to the items like you can do with pages.

share|improve this question

3 Answers 3

You can create template for custom post types by creating files, like for example :


See Template Hierarchy in the codex.

PS : this has already been answered.

share|improve this answer
Thanks for that, but what I would like to know is if it is possible to add custom templates to custom post types. So for instance can I create two templates and assign each template to it's respective post? That only allows for specifying one template file to handle that specific post type as far as I understand it. –  Odyss3us Jul 21 '11 at 17:05
Is see, if you need different templates for single posts, you may want to create several custom post types, according to each needed template. I guess that depends on how many different templates you need. what are you going to do in the templates that needs to be different on each post ? –  mike23 Jul 22 '11 at 10:03

Yes you can do it, it is a little complex to write here so my anser will be links.


http://www.wpbeginner.com/wp-themes/create-custom-single-post-templates-for-specific-posts-or-sections-in-wordpress/ ( this is for posts but you can modify it for CPT's)

http://www.nathanrice.net/blog/wordpress-single-post-templates/ ( this is for posts but you can modify it for CPT's)

It's a good idea for a plugin actually.

share|improve this answer

Here's what works for me:

add_filter('single_template', function($original){ global $post; $post_name = $post->post_name; $post_type = $post->post_type; $base_name = 'single-' . $post_type . '-' . $post_name . '.php'; $template = locate_template($base_name); if ($template && ! empty($template)) return $template; return $original; });

Thus given a post of custom post type foobar and a slug of hello-world the above code would load the single-foobar-hello-world.php template, if it existed.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.