您可以筛选返回的包含路径get_included_files
通过从列表中删除不在主题(和/或子主题)目录中的任何文件:
function get_theme_includes() {
$includedfiles = get_included_files();
// normalize theme paths for matching
$styledir = str_replace("\\\\","/",get_stylesheet_directory());
$templatedir = str_replace("\\\\","/",get_template_directory());
$i = 0; // loop included files
foreach ($includedfiles as $includedfile) {
// normalize include path for match
$includedfile = str_replace("\\\\","/",$includedfile);
// check if included file is in stylesheet directory
if (substr($includedfile,0,strlen($styledir)) != $styledir) {
// if stylesheet is same as template, not a child theme
if ($styledir == $templatedir) {unset($includedfiles[$vi]);}
else {
// check if included file is in template directory
if (substr($includedfile,0,strlen($templatedir)) != $templatedir) {unset($includedfiles[$i]);}
else {
// strip template directory from include path
$pathinfo = pathinfo(str_replace(dirname($templatedir),\'\',$includedfile));
// add filename.php => pathinfo array to the template array
$themeincludes[$pathinfo[\'basename\']] = $pathinfo;
}
}
} else {
// strip stylesheet dir from include path
$pathinfo = pathinfo(str_replace(dirname($styledir),\'\',$includedfile));
// add filename.php => pathinfo array to the template array
$themeincludes[$pathinfo[\'basename\']] = $pathinfo;
}
$i++;
}
return $themeincludes;
}
根据此功能何时运行,您将得到不同的结果,您可以利用此功能进一步删除早期主题包含,因为这些可能不是页面模板。
add_action(\'wp_loaded\',\'check_theme_includes\');
add_action(\'wp_footer\',\'check_theme_templates\');
function check_theme_includes() {
global $themeincludes; $themeincludes = get_theme_includes();
}
function check_theme_templates() {
global $themeincludes, $templateincludes;
$templateincludes = get_theme_includes();
// strip out already included theme files from template list
foreach ($templateincludes as $template => $pathinfo) {
if (array_key_exists($template,$themeincludes)) {
if ($pathinfo[\'dirname\'] == $themeincludes[$template][\'dirname\']) {
unset($templateincludes[$template]);
}
}
}
// debug point
// echo "<!-- INCLUDED TEMPLATES: "; print_r($templateincludes); echo "-->";
// output template array for use by jquery/ajax
echo "<script>var templatenames = new Array(); var templatepaths = new Array(); ";
$i = 0;
foreach ($templateincludes as $template => $pathinfo) {
// optionally strip the .php extension
$template = str_replace(\'.php\',\'\',$template);
// output the template array key/value
echo "templatenames[".$i."] = \'".$pathinfo[\'filename\']."\'; ";
echo "templatepaths[".$i."] = \'".$pathinfo[\'dirname\']."\'; ";
$i++;
}
echo "</script>";
}
例如,在您的页脚中会出现如下内容:
<script>var templatenames = new Array(); var templatepaths = new Array(); templatenames[0] = \'front\'; templatepaths[0] = \'/bioship/sidebar\'; templatenames[1] = \'subfront\'; templatepaths[1] = \'/bioship/sidebar\'; templatenames[2] = \'index\'; templatepaths[2] = \'/bioship\'; templatenames[3] = \'header\'; templatepaths[3] = \'/bioship/sidebar\'; templatenames[4] = \'loop-hybrid\'; templatepaths[4] = \'/bioship\'; templatenames[5] = \'content\'; templatepaths[5] = \'/bioship/content\'; templatenames[6] = \'loop-nav\'; templatepaths[6] = \'/bioship/content\'; templatenames[7] = \'footer\'; templatepaths[7] = \'/bioship/sidebar\'; </script>
这将允许您比较这些数组,并使用jQuery/javascript回调添加到这些数组中。