Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hello and happy new year

Suppose one has a large theme composed of several post types, page templates and a bunch of custom loops and template parts which is best keeping in separate php files. What's the best practice to organise these files?

For example, suppose I have 10 page templates. How about putting them in a folder named "pages"? And another folder for the archives?

However, how to let Wordpress know where to look for the templates?

Thanks

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

Page templates are special case and since WP 3.4 can be put in subfolder natively. Other than that WP mostly expects flat file structure for templates.

While template hierarchy is easily adjusted (see dynamic filter in get_query_template(), pretty much only thing needed)... From personal experience - overly extensive and nested directory structure for templates makes developer workflow overly unpleasant very fast.

If you have extreme quantity of templates "how to reduce amount?" is better question than "how to stuff them into folders?".

share|improve this answer
add comment

To further what Rarst stated, named templates can be placed in a subfolder, as can partials (those consumed via get_template_part). In my experience, the built-in templates that WordPress normally looks for, such as header.php, single.php, and home.php, among others, must be located in the template root folder, not a subfolder.

That said, my theme directory structure tends to look like:

  • partials/
    • nav.php
  • templates/
    • custom_named_template.php
  • header.php
  • home.php
  • single.php

(omitted listing all the standard templates for brevity)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.