Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

up vote 12 down vote accepted

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.

share|improve this answer
    
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 '13 at 13:30
1  
Fortunately it's possible to edit the Codex so the next person won't have the same problem. :-) –  Dalton Feb 13 '13 at 17:34

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.

share|improve this answer

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 );
share|improve this answer

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.