1

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

2 Answers 2

5

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

0
1

There are several way to acchieve this:

    TEMPLATEPATH // Path to your (parent) themes root dir
    STYLESHEETPATH // Path to your parent/child - if present - dir
    get_template_directory_uri();
    get_template_directory();
    get_stylesheet_directory();
    get_stylesheet_directory_uri();

    // 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' );
        }
        else
        {
            if ( file_exists( $dir.$file_name.'-'.$suffix.'.php' ) )
               require_if_theme_supports( $supported, $dir.$file_name.'-'.$suffix.'.php' );
        }
    }
    return;
}

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

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.