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?
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:As explained by @StephenHarris, the template loader does not do this by default.
While Stephen’s answer looks fine to me, I was working on one before he posted and it is a bit different. I used the
archive_template
hook and generated the template name usingget_queried_object
.It is rough code. It may be buggy.