我正在尝试遍历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 );
}我没有收到错误消息,但文件未加载
发布于 2014-08-20 20:25:49
如果你检查the Wordpress codex of locate_template(),你会发现这个函数有3个参数:
locate_template( $template_names, $load, $require_once )$template_names:应为要搜索for$load:布尔值的文件/模板数组,如果设置为true将在found$require_once:布尔值时加载文件,如果设置为true将使用require_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 );https://stackoverflow.com/questions/21591010
复制相似问题