I have a sub-directory in my WordPress theme (there are a lot of pages and I want a more logical structure). I have a custom post type called “entry” but if I place the archive-entry.php template in my sub-directory it is no longer picked up by the theme.

Is there a way I can put my custom post type archive templates in a sub-directory?

2 Answers
2

Unlike page templates, WordPress doesn’t look in subdirectories for post template pages. The base function used for locating such templates is locate_template() (see source), which looks in the root directory of your theme (and parent theme).

(On the other hand get_page_templates() searches sub-directory).

You can use template_include filter (which filters the absolute path to your template) to change the template used:

add_filter( 'template_include', 'wpse119820_use_different_template' );
function wpse119820_use_different_template( $template ){

   if( is_post_type_archive( 'entry' ) ){
       //"Entry" Post type archive. Find template in sub-dir. Look in child then parent theme

       if( $_template = locate_template( 'my-sub-dir/archive-entry.php' ) ){
            //Template found, - use that
            $template = $_template;
       }
   }            


   return $template;
}

Leave a Reply

Your email address will not be published. Required fields are marked *