I have two custom post types, their slugs are: slug-a and slug-b.

I have created two identical single template: single-slug-a.php and single-slug-b.php.

This obviously works as expected.

Is there any way to create just one PHP file for both my post types (ex. single-slug-a-and-slug-b.php)?


I've found this solution:

add_filter( 'template_include', function( $template ) 
    // your custom post types
    $my_types = array( 'slug-a', 'slug-b' );
    $post_type = get_post_type();

    if ( ! in_array( $post_type, $my_types ) )
        return $template;

    return get_stylesheet_directory() . '/single-my-template.php'; 

Is there a more correct way?

1 Answer 1


The other option would be to create the template as a Page Template, and have both CPTs support page-attributes so they can then use Page Templates.

Typically you name the template something like tpl-yourname.php and it would contain a comment at the top:

* Template Name: Your Name
* Template Post Type: cpt-slug-a, cpt-slug-b

The template name will be what you see in the dropdown menu when you are editing one of the CPTs. The template post type needs to contain the names of the post types - i.e. a WP Core Post you would specify as "post" and a CPT called Portfolio you would specify as "portfolio".

You also have to tell each CPT to support page-attributes so that the editor will show the template dropdown.

    // lots of other code here
    // this just shows the line you need to add "page-attributes" to
    'supports' => array('title', 'editor', 'page-attributes');
    // lots of other code here

Just add page-attributes to the array, leaving whatever other things your CPTs support.

As to which way is "correct," I think that's a matter of preference. To me personally this way of using Page Templates seems a little more flexible, since non-technical people can then easily apply the template to whatever pages they like in the editor, and your original way requires coding to apply the template to any additional pages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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