How do i structure my theme folder to avoid one huge list of files

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.

Read More

How can i organise my code better?

Related posts

Leave a Reply

2 comments

  1. 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()
    );