WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 1 down vote accepted

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

~/wp-content/themes/theme_folder/subdir/part-suffix.php

Slightly off topic.

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

get_template_part( 
     'content'
    ,get_post_format()
);
share|improve this answer
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 '12 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 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/

share|improve this answer
    
Yep, good point. I saw that in my investigations. – Will Sep 19 '12 at 4:18

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.