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

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

Related posts

Leave a Reply

3 comments

  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.

  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 )
                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 );
    
  3. 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.