在 WordPress 中将自定义函数放在哪里?
我正在拔头发。我创建了一些简单的函数来生成随机数,这些随机数是从我想在 WordPress 页面上使用的数据库中提取的。 (然后从主题文件中调用它们,例如 header.php 或 page.php。)
我尝试将函数放入主题中的functions.php 中(根据我读过的文档),但我不断得到“调用未定义函数”错误!我到底做错了什么?
例如,这里是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文件中调用它
<?php echo randomPollNumber(); ?>
,是的,我确实尝试使用if_function_exists,但它当然无法找到该函数,所以它当然不会存在。请帮忙?
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.)
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?
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;
}
And I am calling it, from the header.php file with this
<?php echo randomPollNumber(); ?>
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
非常奇怪 - 一些调试技巧:
Very strange - some debugging tips: