有没有办法获取页面中使用的所有模板部件的名称或插件?

时间:2016-05-20 作者:Prafulla Kumar Sahu

我正在开发一个自定义插件,其中我使用单页模板并仅使用ajax更改模板部分,我想检索模板部分slug或name(如custom-header.php、custom-footer.php、custom-sidebar.php、custom-profile.php等)。

是否有任何可能的方法来获取页面中使用的所有模板部件名称?

类似于get_included_files() 我们不能在wordpress中使用它,因为它可能会显示包含的数百个文件,对我来说是156:P和非关联数组:P

1 个回复
最合适的回答,由SO网友:majick 整理而成

您可以筛选返回的包含路径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回调添加到这些数组中。