32

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: http://codex.wordpress.org/Function_Reference/get_template_part

3 Answers 3

51

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.

3
  • Thanks! It's so simple that it's a shame that I didn't discover this. I was convinced that this is not possible because Codex doesn't mention that. This question brought more interesting answers but this one is the simplest so it's probably the most useful one to average person :) (so let it be marked with green tick).
    – Paul
    Feb 6, 2013 at 13:30
  • 1
    Fortunately it's possible to edit the Codex so the next person won't have the same problem. :-) Feb 13, 2013 at 17:34
  • @Sebastien You can actually ex: <?php get_template_part('partials/file'); ?> May 15, 2019 at 23:00
4

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 )
            continue;
        if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
            $located = STYLESHEETPATH . '/' . $template_name;
            break;
        } else if ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
            $located = TEMPLATEPATH . '/' . $template_name;
            break;
        }
    }

    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 );
2
  • Interesting shortcut, I wonder if it has any negative consequences on the loading sequence or file contents.
    – lowtechsun
    Sep 30, 2017 at 0:42
  • get_template_part sets $require_once to false for a reason - OP wants to use the function to call reusable parts... Jun 2, 2020 at 7:06
1

The notes of function get_template_part() says:

Notes
- 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.

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.