【问题标题】:Adding custom php file in wordpress theme在 wordpress 主题中添加自定义 php 文件
【发布时间】:2012-02-23 12:07:43
【问题描述】:

我想在我的wordpress主题中添加一个额外的php文件,例如test.php,并使用example.com/wordpress/test.php之类的url访问它

当我将此 test.php 文件放在主题目录中时,我无法访问它。 那么我该怎么做呢?只有当我将 test.php 放在 wordpress 根目录中时它才允许访问,但我想将文件添加到主题目录而不是根目录中,它位于

wordpress\wp-content\themes\mytheme\test.php

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    避免原生 php 函数

    1. 它们不是相对的,所以破坏东西很容易。 (就像在 OP 案例中一样)
    2. 如果核心有适合您的功能,为什么要自己扮演?
    3. 他们不考虑极端情况

    你不应该使用常量

    • STYLESHEET_DIRECTORY
    • TEMPLATE_DIRECTORY
    • WP_PLUGIN_DIR
    • 等等……

    记住:那些不是 API!因此可以改变!

    请改用以下 API 函数:

    • 家长主题:get_template_directory() / get_template_directory_uri()
    • 儿童主题:get_stylesheet_directory() / get_stylesheet_directory_uri()

    您可以在插件中同时使用两者

    • plugin_dir_path(__FILE__).'/your/path/to/file.ext *) 也适用于主题
    • plugin_dir_url( __FILE__ ).'/your/path/to/file.ext

    plugin_dir_url() 只为插件保留。

    记住...

    • 插件
    • MU 插件
    • 插件
    • 主题
    • 通过register_theme_directory( $dir ); 注册的目录中的主题

    所以请坚持使用 wp_upload_dir();content_url(); 等原生 API 函数。

    一般来说:查看/wp-includes/link-template.php 以获得一大堆不错的路径和与 URl 相关的 API 函数。

    【讨论】:

    • 感谢@Kaiser,为我工作!子主题:
    猜你喜欢
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多