Reputation: 1122
I am building a theme from scratch and I want to have all my custom page templates and single post type templates in a sub folder (see below).
-theme/
- page.php
- single.php
-- templates/
--- page-news.php
--- single-report.php
However, when I have moved page-news.php
to this folder the "Default Template" does not pick up the template.
N.B. I am using WordPress 4.6.1 and using a theme I've built from scratch.
Upvotes: 3
Views: 2778
Reputation: 19366
As far as i know WordPress will not automatically recognize any of your templates if you put them into a subfolder. I guess furthermore that there is not hook to fix this. However you can search the core and try to modify this.
What I recommend is the following: Do not use the WordPress auto detection of template files it is quite clumsy anyway. In your index.php file use conditional tags in order to route WordPress correctly.
https://codex.wordpress.org/Conditional_Tags
This way your are 10 times more flexible regarding subfolders as well as code: You do not need to copy/paste code within the template files.
Use
get_template_part(..)
within your routing functions.
I have built hundreds of WordPress sites and I always do it this way. It is also recommended in some WordPress books.
Note: The loading mechanism of WP is shown within the following image: https://developer.wordpress.org/files/2014/10/wp-hierarchy.png
Update 2018: It is possible to put page templates which are selectable in the post edit screen into a /page-templates/
subfolder and they are detected automatically.
Upvotes: 2
Reputation: 1702
You can only define templates
in the root of your themes. I generally manage all my partial templates as following:
-Theme
-- Templates Folder (partial templates etc.)
--"news-template.php" file (I require/load all the templates I need here.)
This way you can keep all the partial templates
in the template folder and structure them as you want. And load them into the template
file in the root of the project.
I hope my explaination is clear enough.
Upvotes: 2