When working with a worpress site with lots of custom post types and lots of page templates, my theme folder is looking messy. What are the accepted best practices to organise code files in WordPress?

a couple of the limitations i'm coming up against are that templates are chosen by name, so i can't group files by prefixing them. Also custom post template files can not be in sub directories. Also header and footer can not be in subdirectories.

How can i organise my code better?

2 Answers 2


Not a full answer, but I can at least answer this question:

Also custom post template files can not be in sub directories.

Everything, that you can load via get_template_part(), can reside in a subfolder:

get_template_part( 'subdir/part', 'suffix' );

It's as easy as that. Now you've your part inside


Slightly off topic.

Then there're some nice tricks, like using the post format or post type as part name:

  • 1
    This is interesting. So i could let WP fall all the way back to single and choose the template part to match the content for all the post types. And then put those in sub directories. Similar for archives. interesting idea. Ill have a play with that. I think that should get me a long way towards tidying things up. thanks
    – Will
    Sep 19, 2012 at 4:21
  • @Will how did you manage to clean your theme up if I may ask? I'm trying to achieve the same, but moving comments.php, sidebar.php, page.php, single.php seems to bring further issues. Do you an example perhaps?
    – trainoasis
    May 11, 2016 at 9:34

In addition to get_template_part() being able to specify a subdirectory, page templates (of the variety that have to be selected) can be put into a subdirectory: http://nacin.com/2012/03/29/page-templates-in-subdirectories-new-in-wordpress-3-4/

  • Yep, good point. I saw that in my investigations.
    – Will
    Sep 19, 2012 at 4:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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