【问题标题】:Using custom functions without including them使用自定义函数而不包含它们
【发布时间】:2012-02-24 23:35:30
【问题描述】:

我注意到我可以在我的 Wordpress 主题中创建一个空白文件, 并使用我在 functions.php 中创建的函数,不包含任何内容。

这怎么可能?

我在functions.php中的函数(

    function getContentFromID($int) {

        $my_id = $int;
        $my_id = get_post($my_id);
        $content = $my_id->post_content;
        $content = apply_filters('the_content', $content);
        $content = str_replace(']]>', ']]>', $content);
        echo $content;
}

仅 test.php 中的内容(如果我将 $scrapeID 替换为例如 135,它将获得正确的内容)

<?php getContentFromID($scrapeID); ?>

【问题讨论】:

    标签: php wordpress function include


    【解决方案1】:

    Wordpress 自动在模板中包含 function.php。见http://codex.wordpress.org/Theme_Development

    【讨论】:

      【解决方案2】:

      因为你的新文件包含在其他文件中,而这个包含你的文件的文件也包含函数文件,所以函数对你的文件是可见的。

      【讨论】:

        【解决方案3】:

        通过设置auto_prepend_file,可以为每个 PHP 文件包含一个 PHP 文件。

        【讨论】:

          【解决方案4】:

          在包含 test.php 之前,其他的东西已经包含了 functions.php。 PHP 将所有内容都放在一个全局范围内,除非您以其他方式告诉它。

          【讨论】:

          • test.php 不包括在内,我只是链接到它。而 "" 是 only 内容。也许你是对的,但是 PHP 怎么知道在哪里可以找到定义的函数呢?
          • 您通过网络服务器将直接链接到磁盘上的物理test.php文件?
          • 嗯,不,对不起,我误解了我创建了一个 wp 页面并使用 test.php 作为模板。然后我链接到 example.com/getContent/?scrapeID=135 我现在明白你是对的,但这发生在哪里?我想在我的项目中使用此功能。谢谢 :)
          • 我不太了解 WP,无法解释如何,只能解释为什么
          猜你喜欢
          • 1970-01-01
          • 2019-10-23
          • 1970-01-01
          • 1970-01-01
          • 2015-07-18
          • 1970-01-01
          • 2012-09-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多