Assign Page Template Within A Custom Post Type

If you’re adding a custom query within the template to load posts, the post type of that page doesn’t have to be your custom post type. You can create a plain vanilla page, create a template for that via page-your_page_slug.php, or via a custom template assigned in the templates dropdown, then query for whatever post type you want via WP_Query.

EDIT – here’s an example using the single_template filter. you’ll have to change the post type and slug to match yours.

function wpa_single_cpt_template( $templates="" ){
    $single = get_queried_object();

    if( 'myCPT' == $single->post_type
        && 'my-special-slug' == $single->post_name )
        $templates = locate_template( 'my-special-template.php', false );

    return $templates;
}
add_filter( 'single_template', 'wpa_single_cpt_template' );

adapted from the example on Template Hierarchy: Filter Hierarchy.

Leave a Comment