include和get_template_part

时间:2013-03-28 18:28:54

标签: php wordpress content-management-system include

php include和wordpress get_template_part之间有什么区别,那么你有后备选项吗?我可以使用php include吗?

2 个答案:

答案 0 :(得分:2)

我最近也想知道这件事,我发现非常有用的文章是:

How to Include and Require Files and Templates in WordPress

本文作者遵循以下准则:

  
      
  • 在主题开发中,我总是使用get_template_part()。
  •   
  • 在插件开发中,我几乎总是使用include_once(),我通常在函数中使用它一次。你可以在我的样板中看到这个。
  •   
  • 如果我正在编写的插件将在一个非常大的网站上使用,那么我使用require_once()。
  •   

<强>更新

根据another article,使用get_template_part()在尝试构建子主题时特别有用,但我想您已经知道:)。

了解差异的最佳方法可能是检查get_template_part()

的源代码

答案 1 :(得分:0)

get_template_part 还允许 child themes 覆盖该文件并包含其他更具体的文件。

例如,如果您使用 get_template_part( 'loop', 'single' ) 并且您的主题只有一个名为 loop.php 的文件,那么 child theme 可以包含一个名为 loop-single.php 的文件,该文件将覆盖您的 loop.php。