WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
wpbeginner.com/wp-themes/… ( this is for posts but you can modify it for CPT's) 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. – Wyck Jul 22 '11 at 1:43

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

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

First create on page named as Items as your wish which display the content from items post types, than create one template file as below and named that template-items. Select that template for the page you have created.

<div class="container">

    <div class="row">

        <div class="col-md-9">
            <div class="panel panel-default text-center">
                <?php $loop = new WP_Query( array( 'post_type' => 'items', 'posts_per_page' => 5 ) ); ?>                        

                        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
                            <?php the_title();?>
                            <?php if(has_post_thumbnail() ) { the_post_thumbnail(); } ?>
                            <?php the_content();?>
                        <?php endwhile; ?>

                <?php wp_reset_query(); ?>                      


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.