使用自定义函数而不包含它们


Using custom functions without including them

我注意到我可以在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中的ONLY内容(如果我用例如135替换$scrapeID,它会得到正确的内容)

<?php getContentFromID($scrapeID); ?>

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

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

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

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