【问题标题】:include and get_template_part包括和 get_template_part
【发布时间】:2013-03-28 18:28:54
【问题描述】:

php include 和 wordpress get_template_part 之间有什么区别,除了后者有备用选项吗?我也可以使用 php 包含来代替吗?

【问题讨论】:

    标签: php wordpress content-management-system include


    【解决方案1】:

    我最近也想知道这个问题,我发现这篇文章非常有用:

    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()的源代码

    【讨论】:

    • 谢谢...但我不明白为什么作者认为 get_template_part('loop' 'standard');是比 include('loop-standard.php'); 更好的解决方案
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      • 1970-01-01
      相关资源
      最近更新 更多