在包含文件中定义的函数打破wordpress

时间:2017-01-18 19:20:23

标签: php wordpress include

这是我的代码:

单location.php

            <?php
            // include start_dates.php
            include('start_dates.php'); // contians the start dates and a helper function contains()
            $crazy = crazyness_rad(); // breaks the page for some reason

start_dates.php

<?php
// simple test function that returns a string...
function crazyness_rad() {
    return 'WLHLHDFLDHFLDHF KJDHF KJDHF KLJDHF K';
}

当我从single-location.php调用此函数时,它会破坏页面...这在wordpress之外工作正常。

为什么我调用一个include中定义的函数会破坏它?感谢您对我的noob问题的帮助。

我得到的错误是:

致命错误:在第51行的C:\ inetpub \ larockwww \ wp-content \ themes \ larock-academy \ single-location.php中调用未定义的函数crazyness_rad()

我刚刚将函数从start_dates.php移动到了single-location.php,它运行正常:

单location.php

            <?php
            // include start_dates.php
            include('start_dates.php'); // contains the start dates and a 


            // Moved into this file
            function crazyness_rad() {
                return 'WLHLHDFLDHFLDHF KJDHF KJDHF KLJDHF K';
            }

            $crazy = crazyness_rad(); // works fine IT seems the include isn't working..

1 个答案:

答案 0 :(得分:1)

似乎文件start_dates.php不在正确的路径中。请包含文件的绝对路径。您还可以尝试使用get_template_part函数来包含该文件。 https://developer.wordpress.org/reference/functions/get_template_part/