wordpress函数文件,如何循环一次?

wordpress函数文件,如何循环一次?,wordpress,loops,foreach,include,require-once,Wordpress,Loops,Foreach,Include,Require Once,我试图循环遍历wordpress functions.php文件中的一些require_once语句,如下所示 // Initial theme setup require_once locate_template('/inc/init.php'); // Register widget areas require_once locate_template('/inc/sidebar.php'); ... 为什么下面的循环不起作用 $theme_includes = array( '/

我试图循环遍历wordpress functions.php文件中的一些require_once语句,如下所示

// Initial theme setup
require_once locate_template('/inc/init.php');

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

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

$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 );
}
我没有收到任何错误消息,但文件未加载

如果您检查,您将发现该函数包含3个参数:

locate_template( $template_names, $load, $require_once )
  • $template\u name
    :应该是要搜索的文件/模板数组 为了
  • $load
    :布尔值,如果设置为true,将在找到文件时加载文件
  • $require\u once
    :布尔值,如果设置为true,将使用require\u once php函数加载文件
  • 因此,在这种情况下,您可以忽略foreach,只需将数组作为第一个参数传递,并将其他两个参数设置为true:

    $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 );
    

    如果在相对路径之前添加$\u SERVER['DOCUMENT\u ROOT'],会发生什么情况?我找到了解决方案。。我使用的不是
    locate\u template
    ,而是
    get\u template\u directory()
    ,但仍然不知道为什么第一个版本不起作用