WordPress“包括TEMPLATEPATH”还是?

时间:2011-06-26 作者:Fnarp

我建造了这样的东西:

Index Container Widgets Area

我为此创建了一个小部件-

Categories Widget - Index Container .php

在以下情况下:

<?php include (TEMPLATEPATH . \'/includes/containers/container-grid-categories.php\'); ?>
<?php ///include (TEMPLATEPATH . \'/includes/containers/container-list-categories.php\'); ?>
例如在/包括/容器中/container-grid-categories.php

这是:

<?php   //include (TEMPLATEPATH . \'/includes/containers/container-grid-categories/grid-thumbs.php\'); ?>
<?php  include (TEMPLATEPATH . \'/includes/containers/container-grid-categories/grid-tumbs-details.php\'); ?>
所以,我的问题是,一切都很好,问题是这是一个很好的方法来实现我所做的(包括TEMPLATEPATH)?或者用另一种方式,如果是,那是什么方式?谢谢

2 个回复
最合适的回答,由SO网友:Chip Bennett 整理而成

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

我建议引用模板文件includes的样式表路径,这样子主题就可以轻松覆盖这些模板零件文件。因此,理想情况下,您应该使用get_stylesheet_directory_uri().

@kaiser列出的所有功能的一些差异/说明:

  • get_stylesheet_directory_uri()/get_template_directory_uri() 返回URLget_stylesheet_directory()/get_template_directory() 返回所有四个*get_*_directory_*() 函数执行SSL检查TEMPLATEPATH/STYLESHEETHATH 是简单常量,返回文件路径,不执行SSL检查get_*_directory_*() 功能优先于使用TEMPLATEPATH/STYLESHEET; 然而,它们并不是真正用于在子目录中定位模板零件文件。在这种情况下,最好的选择是使用locate_template() 直接地(See here for a great writeup and comparison of all of the above options.)

SO网友:kaiser

有几种方法可以实现此目的:

    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\' );

结束