首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >过滤WordPress中模板文件的PHP输出

过滤WordPress中模板文件的PHP输出
EN

Stack Overflow用户
提问于 2013-05-23 20:44:41
回答 1查看 808关注 0票数 2

基于PHP的基本知识和互联网上的一些智能片段,我已经为WP的子主题开发整理了一个调试例程。它的想法是,它往往很难弄清楚什么进入汤,构成一个呈现的WP页面。

所以我用Rarst添加了这个函数:

代码语言:javascript
复制
/**
 * Function to list all templates used in a page
 * @author Rarst
 * @uri http://wordpress.stackexchange.com/a/89005
 */

function thelist() {
    $included_files = get_included_files();
    $stylesheet_dir = str_replace( '\\', '/', get_stylesheet_directory() );
    $template_dir   = str_replace( '\\', '/', get_template_directory() );

    foreach ( $included_files as $key => $path ) {

        $path   = str_replace( '\\', '/', $path );

        if ( false === strpos( $path, $stylesheet_dir ) && false === strpos( $path, $template_dir ) )
            unset( $included_files[$key] );

        echo $key." = ". $path."</br>";
    }
}

很有用,是的,绝对有用。我将它封装在一个函数中,并在页脚中调用它,就在WP页脚之前(以避免数百个插件文件引用)。把所有这些放在一个CSS三明治里然后包装显示器..。

代码语言:javascript
复制
<?php if (current_user_can('manage_options')) { ?>
    <div class="debug">
        <?php thelist(); ?>
    </div>
<?php } ?>

现在我们有进展了。不幸的是,在某个地方,我要“伙计,我需要的文件总是在页面的尽头3英里处”--但是过滤函数输出会让我非常沮丧。所以我来到这里是为了了解它是如何完成的,以及为什么,并把这一点的智慧向前推进。

来自随机客户端站点的一些示例输出:

代码语言:javascript
复制
0 = /path/to/file/index.php
1 = /path/to/file/wp-blog-header.php
2 = /path/to/file/wp-load.php
3 = /path/to/file/wp-config.php
4 = /path/to/file/wp-settings.php
5 = /path/to/file/wp-includes/load.php
6 = /path/to/file/wp-includes/default-constants.php
7 = /path/to/file/wp-includes/version.php
8 = /path/to/file/wp-includes/compat.php
9 = /path/to/file/wp-includes/functions.php
10 = /path/to/file/wp-includes/option.php
[...]
219 = /path/to/file/wp-content/plugins/advanced-custom-fields/core/fields/page_link.php
220 = /path/to/file/wp-content/plugins/advanced-custom-fields/core/fields/post_object.php
221 = /path/to/file/wp-content/plugins/advanced-custom-fields/core/fields/relationship.php
222 = /path/to/file/wp-content/plugins/advanced-custom-fields/core/fields/taxonomy.php
223 = /path/to/file/wp-content/plugins/advanced-custom-fields/core/fields/user.php
224 = /path/to/file/wp-content/plugins/advanced-custom-fields/core/fields/date_picker/date_picker.php
225 = /path/to/file/wp-content/plugins/advanced-custom-fields/core/fields/color_picker.php
226 = /path/to/file/wp-content/plugins/advanced-custom-fields/core/fields/message.php
227 = /path/to/file/wp-content/plugins/advanced-custom-fields/core/fields/tab.php
228 = /path/to/file/wp-content/plugins/add-to-any/add-to-any-wp-widget.php
229 = /path/to/file/wp-includes/class-wp-admin-bar.php
230 = /path/to/file/wp-includes/template-loader.php
231 = /path/to/file/wp-content/themes/themename/category.php
232 = /path/to/file/wp-content/themes/themename/header.php
233 = /path/to/file/wp-content/themes/themename/content.php
234 = /path/to/file/wp-content/themes/themename/sidebar.php
235 = /path/to/file/wp-content/themes/themename/footer.php

基本上,我需要235行输出中的最后5行。更具体地说,我需要过滤出路径中有/wp内容/主题/的任何内容,并且只显示它。

唯一的问题是我还在学习语法和类文件。我还没有真正进入Regex和过滤..。但我很希望这份清单能更实用一些。最后,我想把过滤过的路径粘在配置文件中,但我不想为自己弄清楚这一点(这就是为什么我要学习使用jQuery和CSS)。

但是,我确实需要你的帮助,一个基本的主题过滤器!

更新: HamZa DzCyberDeV钉死了

代码语言:javascript
复制
if(!strpos($path, '/wp-content/themes/') === false) {
    echo $key." = ". $path."</br>";
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-23 21:15:55

因此,正如评论中所述,您可以执行以下操作:

代码语言:javascript
复制
if(!strpos($path, '/wp-content/themes/') === false) {
    echo $key." = ". $path."</br>";
}

为了学习目的,使用regex:

代码语言:javascript
复制
if(!preg_match('#/wp-content/themes/#i', $path) === false) {
    echo $key." = ". $path."</br>";
}

i修饰符:不区分大小写。

在线演示

学习正则表达式硬道

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16723569

复制
相关文章

相似问题

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