# Same template for several custom post types

**URL:** <https://discourse.roots.io/t/same-template-for-several-custom-post-types/1414>\
**Category:** uncategorized\
**Created:** [March 19, 2014, 9:54am UTC](https://discourse.roots.io/t/same-template-for-several-custom-post-types/1414 "2014-03-19T09:54:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![antosusc](https://discourse.roots.io/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@antosusc](https://discourse.roots.io/u/antosusc)\
**Post date:** [March 19, 2014, 9:54am UTC](https://discourse.roots.io/t/same-template-for-several-custom-post-types/1414/1 "2014-03-19T09:54:39Z")

</div>

Hello

I have several custom post types that share archive and single templates but being different from the usual single post.

The WordPress way would be to create one archive and single php for each CPT with the same content. But which would be the roots DRY way? Should I modify base.php? Or should I do some kind of

```php
    if ( "cpt1" === $post->post_type
            || "cpt2" === $post->post_type
            || "cpt3" === $post->post_type
            || "cpt4" === $post->post_type
            || "cpt5" === $post->post_type
          ) :
    
      get_template_part( 'templates/content', 'archive-cpts' );

```

in index.php and something similar in single.php?

Any other suggestion?

Thanks

---

<div class="post-metadata">

**Author:** ![Foxaii](https://discourse.roots.io/user_avatar/discourse.roots.io/foxaii/32/6289_2.png) [@Foxaii](https://discourse.roots.io/u/Foxaii)\
**Post date:** [March 19, 2014, 11:33am UTC](https://discourse.roots.io/t/same-template-for-several-custom-post-types/1414/2 "2014-03-19T11:33:51Z")

</div>

It’s a judgement call. For minor changes between templates, I would use a conditional. For major changes I would use a new template.

In your case I would probably create archive-custom.php and filter `template_include` to get the CPTs to use it. With the singles I would probably just edit single.php and load the custom content via `get_template_part`.

I would definitely tidy up your logic and remove it from the templates though, so that it can be reused.

---

<div class="post-metadata">

**Author:** ![antosusc](https://discourse.roots.io/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@antosusc](https://discourse.roots.io/u/antosusc)\
**Post date:** [March 19, 2014, 8:53pm UTC](https://discourse.roots.io/t/same-template-for-several-custom-post-types/1414/3 "2014-03-19T20:53:02Z")

</div>

You mean with my own wrapper and priority lower than 99 right??  
Thanks a lot

---

<div class="post-metadata">

**Author:** ![JulienMelissas](https://discourse.roots.io/user_avatar/discourse.roots.io/julienmelissas/32/6132_2.png) [@JulienMelissas](https://discourse.roots.io/u/JulienMelissas)\
**Post date:** [March 20, 2014, 1:58pm UTC](https://discourse.roots.io/t/same-template-for-several-custom-post-types/1414/4 "2014-03-20T13:58:22Z")

</div>

I also think minor changes can go into the file, until it starts to look messy, then you’ll need to move it to new files.

---

<div class="post-metadata">

**Author:** ![Foxaii](https://discourse.roots.io/user_avatar/discourse.roots.io/foxaii/32/6289_2.png) [@Foxaii](https://discourse.roots.io/u/Foxaii)\
**Post date:** [March 20, 2014, 1:59pm UTC](https://discourse.roots.io/t/same-template-for-several-custom-post-types/1414/5 "2014-03-20T13:59:41Z")

</div>

Not sure what you mean by your own wrapper, but the priority needs to be less than 99 for the Roots wrapper to find the appropriate base file.

---

<div class="post-metadata">

**Author:** ![antosusc](https://discourse.roots.io/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@antosusc](https://discourse.roots.io/u/antosusc)\
**Post date:** [March 20, 2014, 6:09pm UTC](https://discourse.roots.io/t/same-template-for-several-custom-post-types/1414/6 "2014-03-20T18:09:43Z")

</div>

The point is that base file is the same one (same heading, logo, footer, etc, but the changing part is the content so: Can I create a function that selects a custom-archive-for-several-cpt.php instead of index.php or archive.php, having just one base.php???

Nevermind, (I leave my previous paragraph just in case it might help others). I just realized that with `template_include` we are modifying what we are calling from base.php `include roots_template_path();`, right? So we have just one base.php.

Please, could you tell me if this is the right way, including the filter call, in /lib/custom.php

```php
    function custom_cpt( $template ) {
    if ( "cpt1" === $post->post_type
            || "cpt2" === $post->post_type
            || "cpt3" === $post->post_type
            || "cpt4" === $post->post_type
            || "cpt5" === $post->post_type
          ) :
         $new_template = locate_template( array( 'custom-archive-for-several-cpt.php' ) );
    		if ( '' != $new_template ) {
    			return $new_template ;
    		}
    	}
    endif;
    
        	return $template;
        }
    
    add_filter('template_include', 'custom_cpt', 98);

```

and then in custom-archive-for-several-cpt.php just the content between

```auto
    <main class="main <?php echo roots_main_class(); ?>" role="main">
    
    </main>

```

Thanks

---

<div class="post-metadata">

**Author:** ![Foxaii](https://discourse.roots.io/user_avatar/discourse.roots.io/foxaii/32/6289_2.png) [@Foxaii](https://discourse.roots.io/u/Foxaii)\
**Post date:** [March 20, 2014, 11:03pm UTC](https://discourse.roots.io/t/same-template-for-several-custom-post-types/1414/7 "2014-03-20T23:03:38Z")

</div>

Rather than access the global variable, you’re probably better off using `get_post_type()` and you don’t need to call it umpteen times.

Either use `in_array` or set it to a variable scoped to the function.

---

<div class="post-metadata">

**Author:** ![antosusc](https://discourse.roots.io/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@antosusc](https://discourse.roots.io/u/antosusc)\
**Post date:** [March 21, 2014, 12:44pm UTC](https://discourse.roots.io/t/same-template-for-several-custom-post-types/1414/8 "2014-03-21T12:44:11Z")

</div>

Perfect, I assume the rest is correct right?  
The `return $template` and `add_filter('template_include', 'custom_cpt', 98);` is the part I had doubts about.

Thanks a lot for your support.

---

<div class="post-metadata">

**Author:** ![Foxaii](https://discourse.roots.io/user_avatar/discourse.roots.io/foxaii/32/6289_2.png) [@Foxaii](https://discourse.roots.io/u/Foxaii)\
**Post date:** [March 21, 2014, 1:50pm UTC](https://discourse.roots.io/t/same-template-for-several-custom-post-types/1414/9 "2014-03-21T13:50:29Z")

</div>

The rest looks fine. The easiest way is to test it on your dev server.

---

<div class="post-metadata">

**Author:** ![antosusc](https://discourse.roots.io/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@antosusc](https://discourse.roots.io/u/antosusc)\
**Post date:** [March 21, 2014, 5:24pm UTC](https://discourse.roots.io/t/same-template-for-several-custom-post-types/1414/10 "2014-03-21T17:24:04Z")

</div>

Perfect. It is working fine:

```php
function template_productos( $template ){
  if ( in_array( get_post_type(), array( "cpt1", "cpt2", "cpt3") ) ) :
    $new_template = locate_template( array( "templates/cpt-template.php" ) );
    if ( "" !== $new_template ) {
      return $new_template ;
    }
    endif;
    return $template;
}
add_filter( 'template_include', 'template_productos', 98 );

```

Really seeing the potential of working like this in terms of organisation and workflow.

I don’t find how to close this thread… Thanks for your support!

---

<div class="post-metadata">

**Author:** ![rpasillas](https://discourse.roots.io/user_avatar/discourse.roots.io/rpasillas/32/5562_2.png) [@rpasillas](https://discourse.roots.io/u/rpasillas)\
**Post date:** [August 2, 2018, 9:21pm UTC](https://discourse.roots.io/t/same-template-for-several-custom-post-types/1414/11 "2018-08-02T21:21:22Z")

</div>

I’m doing a very similar thing. Which is to render the same template file for the singular view of a few different post types. It works fine, however, the Show Current Template plugin thinks single.blade.php template is being used instead. It all works fine, but it’s just a little confusing. Am I doing this right?

```
add_filter('template_include', function( $original_template ) {

    if ( is_singular( ['audio-video', 'print', 'brands', 'digital-social', 'integrated', 'experiential'] ) ) {
        return locate_template( ['views/project-single.blade.php'] );
    } else {
        return $original_template;
    }
});
```
