There are a number of template filters you can use to alter the template hierarchy. Have a look at the template hierarchy page at the filters and example provided.
Here’s a modified version of the example that uses single_template
. It checks for a specific custom post type and loads the theme’s page.php
template if it exists:
function wpa54721_single_template( $templates="" ){
// get data for the current post and check type
$this_post = get_queried_object();
if( 'my_custom_post_type' == $this_post->post_type ):
// $templates could be empty, a string, or an array,
// we need to check and alter it appropriately
if( !is_array( $templates ) && !empty( $templates ) ):
// it's a string
$templates = locate_template( array( 'page.php', $templates ), false );
elseif( empty( $templates ) ):
// it's empty
$templates = locate_template( 'page.php', false );
else:
// it's an array
$new_template = locate_template( array( 'page.php' ) );
if( !empty( $new_template ) ) array_unshift( $templates, $new_template );
endif;
endif;
return $templates;
}
add_filter( 'single_template', 'wpa54721_single_template' );