在WordPress中加载模板文件的5种方式及其差异


WordPress具有读取模板文件的多种功能,我不知道有什么区别,因此我进行了查找。

1.包括

首先,使用include,该文件读取PHP自身功能的外部文件。

1
include(TEMPLATEPATH . '/template-name.php');

优点:最快
缺点:不要检查文件

是否存在

2. load_template()

接下来是load_template(),从这里开始WordPress功能

1
load_template(TEMPLATEPATH . '/template-name.php');

优点:高速
缺点:毕竟,它不检查文件是否存在。您必须使用完整路径编写它。

3.locate_template()

然后如何使用locate_template()

1
locate_template($template_names, $load);

优点:
它不必是绝对路径,因为它会搜索文件。
您也可以包含文件。如果$ load是true,请读取它,如果它是false,则只需添加路径。

$ template_names是包含模板名称的数组。如果此处已经存在,将不包括在内。

4. get_query_template()

我已经厌倦了写作。

…接下来是如何使用get_query_template()

1
include(get_query_template('template-name'));

它会读没有扩展名,我做到了!

5 get_template_part()

可以从Wordpress 3.0使用的功能。它非常强大,或者也许是我基本上只使用的一种。到目前为止的解释是什么...

1
2
3
get_template_part('loop'); // general loop, file 'loop.php'
get_template_part('loop', 'index'); // loop for index, file 'loop-index.php'
get_template_part('loop', 'single'); // loop for single post, file 'loop-single.php'

您可以像

一样阅读它。

参考

在WordPress中包括模板文件的5种方法-豪华博客提示
http://www.deluxeblogtips.com/2010/06/wordpress-include-template-files.html