Two post types with same single template

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:

<?php
/*
* 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.

register_post_type('cpt-slug-a',
    // 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.

tech