【发布时间】:2020-07-07 16:57:40
【问题描述】:
我是 WordPress 的新手,我尝试使用 ajax 创建实时搜索,我希望当用户按下按钮时会出现一个框,并且搜索结果会显示在该框中。
为此,我在我的主题文件夹中创建了一个名为 livesearch 的文件夹,在该文件夹中我放置了一个名为 search-live.php 的文件,该文件中的代码如下所示:
<?php
//echo $_GET['search'];
$test = GetMySearchResult($_GET['search']);
print_r($test);
在我的 functions.php 文件中,我创建了一个名为 GetMySearchResult 的函数,如下所示:
function GetMySearchResult($search){
global $wpdb;
$myrows = $wpdb->get_results( "SELECT * FROM wp_posts post_type = post ID LIKE %" . $search . "%" );
return $myrows;
}
add_action('init', 'GetMySearchResult');
我使用 ajax 和代码向search-live.php 发送数据,如下所示:
<script>
var GetSearch = document.getElementById('search');
GetSearch.addEventListener("keyup", function(){
//InfoData = {slug:GetSearch.value}
$.ajax({
type: "GET",
url: '<?php echo get_site_url(); ?>/wp-content/themes/webranko/livesearch/search-live.php?search=' + GetSearch.value ,
data: '',
datatype: "html",
success: function(result) {
console.log(result);
}
});
});
</script>
我已经 $_GET['search']; 并且一切正常,我可以在控制台中看到结果,但是当我调用 GetMySearchResult($_GET['search']) 时,它给了我一个像这样的致命错误:
致命错误:未捕获错误:调用 C:\xampp\htdocs\web\wp-content\themes\webranko\livesearch\search-live.php:3 中的未定义函数 GetMySearchResult() 堆栈跟踪: #0 {主要} C:\xampp\htdocs\web\wp-content\themes\webranko\livesearch\search-live.php 第 3 行
what我错过了还是我做错了什么?
【问题讨论】:
标签: php wordpress wordpress-theming