1

我在archive.php 中写了一个“自动完成”字段——输入它会调用functions.php 中定义的函数。这是代码示例:

function ajax_search_archive() {
$term = get_queried_object();
if (!$term) return;

(... continued...)
}

我在“标签”存档页面中尝试过,然后在“类别”存档页面中尝试过。每次调用该函数,然后在第二行返回 0,因为检索到的“get_queried_object”为空——尽管我预计它不是。

请问,我做错了什么?

4

1 回答 1

1

Thanks again for the comments, which showed me the way to solve my own problem.

The function get_queried_object() seems to work when called in archive.php itself, but not when called from a function defined in another file - even if that other function is called in archive.php. That realization led me to do some updates:

a) in archive.php: retrieving the get_queried_object(), storing its id into a hidden field (e.g. "archive_id)";

b) the Ajax "autocomplete" call in its script: sending the object id as another argument (value retrieved using jQuery - e.g. "$('#archive_id').val()");

c) the function ajax_search_archive in functions.php: retrieving the object id from the POST request.

于 2018-08-24T13:06:02.587 回答