WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I built something like this:

Index Container Widgets Area

and I created a widget for that -

Categories Widget - Index Container .php

with this in:

<?php include (TEMPLATEPATH . '/includes/containers/container-grid-categories.php'); ?>
<?php ///include (TEMPLATEPATH . '/includes/containers/container-list-categories.php'); ?>

and for example in the /includes/containers/container-grid-categories.php

is this:

<?php   //include (TEMPLATEPATH . '/includes/containers/container-grid-categories/grid-thumbs.php'); ?>
<?php  include (TEMPLATEPATH . '/includes/containers/container-grid-categories/grid-tumbs-details.php'); ?>

So, my questions, with this everything is working fine, the question is this a good way to make what I did (include TEMPLATEPATH)? Or use another way, and if that what way? Thanks

share|improve this question
up vote 4 down vote accepted

Long answer short: the absolute-best answer, for template-part files in a subdirectory, is to use locate_template()

I would recommend referencing the stylesheet path for template file includes, so that those template part files can be easily over-ridden by Child Themes. So, ideally, you should use get_stylesheet_directory_uri().

Some differences/explanation of all the functions listed by @kaiser:

  • get_stylesheet_directory_uri()/get_template_directory_uri() return a URL
  • get_stylesheet_directory()/get_template_directory() return a file path
  • All four of the *get_*_directory_*() functions perform SSL checking
  • TEMPLATEPATH/STYLESHEETHATH are simple constants, return a file path, and do not perform SSL checking

So, the four get_*_directory_*() functions are preferred to using TEMPLATEPATH/STYLESHEET; however, they aren't really intended for locating template part files in subdirectories. In such a case, the best option is to use locate_template() directly. (See here for a great writeup and comparison of all of the above options.)

share|improve this answer
+1 for concise, clear answer – TheDeadMedic Jun 26 '11 at 16:16

There are several way to acchieve this:

    TEMPLATEPATH // Path to your (parent) themes root dir
    STYLESHEETPATH // Path to your parent/child - if present - dir

    // to load a file: path_and_file_name.php which searches in child dir first, then parent themes dir
    get_template_part( 'path_and_file_name' ); // appends .php automagically
    get_template_part( 'path_and_file_name', 'suffix' ); // loads: path_and_file_name-suffix.php

 * Directoy structure
 * @return (array) $dir_struct | directory structure
function wpse21093_get_dir_struct()
    $dir_base  = get_template_directory().'/includes/';
    $dirs[] = 'containers';
    // add more folders
    // $dirs[] = 'widgets';
    foreach ( $dirs as $dir )
        $dir_struct[ $dir ] = trailingslashit( $dir_base.$dir );
    return $dir_struct;

 * Loading files for larger structures the *smart* way ...
 * Searches in all folders supported by the theme, defined by the user
 * Allows a {$suffix} to be appended to mimic the get_template_part(); behavior
function wpse21093_load_file( $file_name, $suffix = '' )
    $dirs = wpse21093_get_dir_struct();
    foreach ( $dirs as $supported => $dir )
        if ( empty ( $suffix ) )
            if ( file_exists( $dir.$file_name.'.php' ) )
               require_if_theme_supports( $supported, $dir.$file_name.'.php' );
            if ( file_exists( $dir.$file_name.'-'.$suffix.'.php' ) )
               require_if_theme_supports( $supported, $dir.$file_name.'-'.$suffix.'.php' );

// Then call it like this:
add_theme_support( 'containers' ); // add support for folder
wpse21093_load_file( 'container-grid-categories' );
share|improve this answer

Your Answer


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.