在 WordPress 中将自定义函数放在哪里?

发布于 2024-11-01 18:10:29 字数 710 浏览 1 评论 0原文

我正在拔头发。我创建了一些简单的函数来生成随机数,这些随机数是从我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

番薯 2024-11-08 18:10:29

非常奇怪 - 一些调试技巧:

  1. 将 die('functions.php file returned') 语句放在functions.php 的开头(而不是在函数内部)。如果它没有消失,您就知道该文件没有被加载。
  2. 如果它死掉了,那么检查你的拼写(将functions.php中的函数名称复制并粘贴到你的echo [...]中)。令人惊讶的是,有多少次我确信自己拼写正确,但事实上却没有。
  3. 如果它没有消失,请检查您的文件是否确实名为functions.php,它是否位于您正在编码的主题的正确主题文件夹中。
  4. 可能是functions.php 文件中有错误,因此没有被解析,因此Wordpress 找不到该函数。检查您的日志是否有错误。仅加载函数文件,然后检查该函数是否正常工作。您使用的是 PHP Unit 还是类似的东西?

Very strange - some debugging tips:

  1. Put die('functions.php file loaded') statement at the beginning of functions.php (not inside a function). If it doesn't die, you know the file isn't being loaded.
  2. If it dies, then check your spelling (copy and paste the function name from functions.php into your echo [...]). It's amazing how many times I'm SURE I've spelt it right, when in fact I haven't.
  3. If it doesn't die, check that your file is definitely called functions.php, that it's definitely inside the right theme folder for the theme you are coding.
  4. It's possible that the functions.php file has an error in it, and so is not being parsed, hence Wordpress can't find the function. Check your logs for errors. Load the functions file and nothing else, and check that the function is working. Are you using PHP Unit or something like that?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文