Is there any way to use get_template_part() with folders?

The question:

I’m wondering if there is any way to use get_template_part() with folders? My main folder has a lot of files now because I put every re-usable element in a separate file. I’d like to put them in folders then.

There is no information about that in Codex:

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

In fact you can, I have a folder in my theme directory called /partials/ in in that folder I have files such as latest-articles.php, latest-news.php and latest-statements.php and I load these files using get_template_part() like:

get_template_part('partials/latest', 'news');

get_template_part('partials/latest', 'articles');

get_template_part('partials/latest', 'statements');

Just dont forget to omit the .php from the file name.

Method 2

I’m afraid not. If in codex isn’t something you would like to know, try to follow the link to the source and have a look yourself to the code and try to manage it out.

I had a look and the get_template_part function is defined as below:

function get_template_part( $slug, $name = null ) {
    do_action( "get_template_part_{$slug}", $slug, $name );

    $templates = array();
    if ( isset($name) )
        $templates[] = "{$slug}-{$name}.php";

    $templates[] = "{$slug}.php";

    locate_template($templates, true, false);

From this, you can read out, that get_template_part function just creates an intended php file name and calls function locate_template. This in not useful, so I had a look also on locate_template function:

function locate_template($template_names, $load = false, $require_once = true ) {
    $located = '';
    foreach ( (array) $template_names as $template_name ) {
        if ( !$template_name )
        if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
            $located = STYLESHEETPATH . '/' . $template_name;
        } else if ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
            $located = TEMPLATEPATH . '/' . $template_name;

    if ( $load && '' != $located )
        load_template( $located, $require_once );

    return $located;

Get locate template searches for php file called from get_template_part. But you can call locate_template directly from you code. And this is useful.

Try out this code instead of get_template_part(‘loop-sigle.php’) function (your file is located in mydir inside your theme):

locate_template( 'mydir/loop-single.php', true, true );

Method 3

The notes of function get_template_part() says:

– Uses: locate_template()
– Uses: do_action() Calls ‘get_template_part_{$slug}’ action.

Wich allows you to make use of locate_template(), wich says:

Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which inherit from a parent theme can just overload one file.

If you define TEMPLATEPATH with the subdirectory you want to use, get_template_part() will search for the files in your subdirectory.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment