0

Is it possible to move 'default' CPT templates (example: single-cpt_name) to a sub-folder and still have WP recognise and auto-assign them?

If so, how?

1 Answer 1

2

I think you should use single_template filter hook to load your template from a subdirectory. The full code will be like below-

function the_dramatist_get_custom_post_type_template($single_template) {
    global $post;

    if ($post->post_type == 'your_post_type') {
        // Your post type directory with the post type template file name
        $single_template = dirname( __FILE__ ) . '/post-type-template.php';
    }
    return $single_template;
}
add_filter( 'single_template', 'the_dramatist_get_custom_post_type_template' );

Please read the documentation for further information. Hope that helps.

1
  • Thanks, excellent. With this the theme root is less cluttered, and the required template can be in a sub-sub-folder (example: templates/cpt-name/template-name.php).
    – glvr
    May 30, 2017 at 7:14

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.