The root directory of your WordPress theme can get pretty crowded pretty fast as a project get steadily more complex. One thing that I found used to frustrate me was differentiating at a glance, my partial template files, from my page template files, from all the others that find their way in there.
De-cluttering your workspace makes it easier to spot items you require at a glance, helping you to avoid losing concentration whilst you’re working.
This becomes especially true when you start collaborating on a project, using task runners and css pre-compilers. All these things can make a theme fairly bulky, so if you know where everything is going to be in advance then it can help to speed up your work process.
Moving Partial Template Files
The easiest modification and usually the first thing I do when modifying or creating a theme from scratch is to separate out my partial template files. Partial templates include
footer.php and perhaps any
loop.php files you may have. Taking all these and moving them to a separate subdirectory where they won’t get in the way.
Retrieving them is as easy as using
get_template_part($slug, $name); and prepending the subdirectory path to the
Header and Footer
The header and footer files are often included in theme files by using there own special function
get_footer(). These functions are essentially the same as the get_template_part() function except the
$slug argument is not required.
Yes, get_header() and get_footer() are arguably easier to remember and slightly faster to type than get_template_part() but I think the benefits of having a clean organised theme folder outweigh the extra keystrokes.
- Part 2 – Organising WordPress Templates – Setting Up a Compass Project in WordPress
- The Codex – get_template_part()
- A good example of this strategy can be found in the excellent Roots theme – GitHub
Other articles in this series
- Organising WordPress Themes – Partial Templates
- Organising WordPress Themes – Setting Up a Compass Project