我的自定义功能放在哪里? [英] Where to put my custom functions in Wordpress?
问题描述
我正在拔头发.我创建了一些简单的函数来生成随机数,这些函数是从要在wordpress页面上使用的数据库中提取的.(然后从主题文件(例如header.php或page.php)中调用它们.)
I am pulling my hair out. I have created some simple functions that generate random numbers, pulled from a database that I want to use on wordpress pages. (And then call them from the theme files, such as header.php or page.php.)
我尝试将函数放入主题中的functions.php中(根据我阅读的文档),但是我不断收到调用未定义函数"错误!我到底在做错什么?
I have tried putting the functions inside functions.php that is in the theme (as per the documentation I have read), but I keep getting the "call to undefined function" errors! What in the world am I doing wrong?
示例,这是functions.php中的一个函数
Example, here is a function inside the functions.php
function randomPollNumber() {
///this gets a random active poll number
$sql12 = "SELECT id FROM poll WHERE removed = 0 AND active = 1 ORDER BY RAND() LIMIT 1";
$result12 = mysql_query($sql12);
$myrow12 = mysql_fetch_row($result12);
$pollquestionid = $myrow12[0];
return $pollquestionid;
}
我正在从header.php文件中调用它
And I am calling it, from the header.php file with this
<?php echo randomPollNumber(); ?>
是的,我DID尝试使用if_function_exists,但是当然它不能查找该函数,因此它不存在.请帮忙吗?
And yes, I DID try using the if_function_exists, but of course it cannot FIND the function, so of course it does not exist. Please help?
推荐答案
非常奇怪-一些调试提示:
Very strange - some debugging tips:
- 在function.php的开头(不在函数内部)放置die("functions.php文件已加载")语句.如果它没有消失,则说明文件没有被加载.
- 如果死了,请检查您的拼写(将来自functions.php的函数名称复制并粘贴到您的echo [...]中).可以肯定的是,我确实拼写正确的次数真是令人惊讶,而实际上却没有.
- 如果它没有消亡,请检查您的文件是否确实称为functions.php,并确保它在正确的主题文件夹内,以适合您正在编码的主题.
- functions.php文件中可能有错误,因此没有被解析,因此Wordpress无法找到该函数.检查您的日志中是否有错误.加载函数文件,然后再加载其他文件,然后检查函数是否正常运行.您使用的是PHP Unit还是类似的东西?
这篇关于我的自定义功能放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!