首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wordpress函数文件,如何循环require_once?

wordpress函数文件,如何循环require_once?
EN

Stack Overflow用户
提问于 2014-02-06 07:36:59
回答 1查看 342关注 0票数 0

我正在尝试遍历wordpress functions.php文件中的一些require_once语句,如下所示

代码语言:javascript
复制
// Initial theme setup
require_once locate_template('/inc/init.php');

// Register widget areas
require_once locate_template('/inc/sidebar.php');

...

为什么下面的循环不起作用?

代码语言:javascript
复制
$theme_includes = array(
  '/inc/init.php', // Initial theme setup
  '/inc/sidebar.php', // Register widget areas
  '/inc/scripts.php', // Register scripts and stylesheets
  '/inc/nav.php', // Main navigation
  '/inc/cleanup.php', // Cleanup
  '/inc/customizer.php',
  '/inc/template-tags.php', // Custom template tags
  '/inc/extras.php', // No theme functions
  '/inc/analytics.php' // Google Analytics
 );

foreach ( $theme_includes as $include ) {
  require_once locate_template( $include );
}

我没有收到错误消息,但文件未加载

EN

回答 1

Stack Overflow用户

发布于 2014-08-20 20:25:49

如果你检查the Wordpress codex of locate_template(),你会发现这个函数有3个参数:

代码语言:javascript
复制
locate_template( $template_names, $load, $require_once )

  1. $template_names:应为要搜索for
  2. $load:布尔值的文件/模板数组,如果设置为true将在found
  3. $require_once:布尔值时加载文件,如果设置为true将使用require_once php函数

加载文件

因此,在这种情况下,您可以忘记foreach,只需将数组作为第一个参数传递,并将其他两个参数设置为true:

代码语言:javascript
复制
$theme_includes = array(
      '/inc/init.php', // Initial theme setup
      '/inc/sidebar.php', // Register widget areas
      '/inc/scripts.php', // Register scripts and stylesheets
      '/inc/nav.php', // Main navigation
      '/inc/cleanup.php', // Cleanup
      '/inc/customizer.php',
      '/inc/template-tags.php', // Custom template tags
      '/inc/extras.php', // No theme functions
      '/inc/analytics.php' // Google Analytics
);

locate_template( $theme_includes, true, true );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21591010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档