WordPress - 如何按字母顺序排列 jQuery 自动完成搜索建议结果,优先第一个字符,然后第二个等?
WordPress - How to alphabetically order jQuery AutoComplete search suggestion results, prioritizing first character, then second etc.?
我将 jQueryUI AutoComplete 与 WordPress 一起使用,以在用户在搜索框中键入内容时获取自定义分类的术语。
我有很多运动品牌作为自定义分类术语,因此,例如术语 Adidas
可以有很多子术语。现在当我输入 a
时,它显示...
Adidas
Adidas Shoes
Adidas Socks
- 等...
...导致其他品牌蒙在鼓里。所以,当我输入 a
时,我希望结果显示如下...
Adidas
Amphipod
Asics
- 等...
...直到我继续键入 ad
,然后才会出现带有子项的 Adidas
结果,就像第一个示例中那样。这有可能实现吗? jQueryUI AutoComplete 是否能够像这样为我排序结果,或者我是否需要在服务器端使用 PHP 函数(如 sort
、usort
等)对它们进行排序?到目前为止,我尝试编写了一些不同的 PHP 排序函数,但都无济于事,但我现在真的不知道。
这是我现在拥有的代码:
autocomplete.js
$(function() {
var url = MyAutocomplete.url + "?action=my_search";
$('.search-field').autocomplete({
source: url,
delay: 500,
minLength: 2,
sortResults: true
})
});
functions.php(WordPress)
function my_search()
{
$args = array(
'search' => strtolower($_GET['term']),
'taxonomy' => array('suggestion_string'),
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'number' => 10,
);
$search_query = new WP_Term_Query($args);
$results = array( );
if ( $search_query->get_terms() ) {
foreach($search_query->get_terms() as $term) {
$results[] = array(
'label' => $term->name,
);
}
}
else {
}
// Tried to write a few different sort functions here to no avail, like:
sort($results);
$data = json_encode($results);
echo html_entity_decode( $data );
exit();
}
add_action( 'wp_ajax_my_search', 'my_search' );
add_action( 'wp_ajax_nopriv_my_search', 'my_search' );
朋友,一切皆有可能,您只需按照自己的意愿编写代码即可。你可以在服务器端和客户端实现你想要的,在这种情况下我会推荐服务器端。
如果您不想自动完成显示子类别,请不要提供这些子类别。您要么首先消除以相同单词开头的名称,要么排除子类别。这是我为您提供的第一种可能性的代码:
function my_search()
{
$args = array(
'search' => strtolower($_GET['term']),
'taxonomy' => array('suggestion_string'),
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'number' => 10,
);
$search_query = new WP_Term_Query($args);
$results = array( );
if ( $search_query->get_terms() ) {
foreach($search_query->get_terms() as $term) {
$results[] = array(
'label' => $term->name,
);
}
}
else {
}
$filtered = [];
$existing = [];
if(strlen($_GET['term'])<2) {
foreach($results as $term) {
$first_word = explode(" ",$term["label"])[0];
if(!in_array($first_word,$existing) {
array_push($existing,$first_word);
$filtered[] = $term;
}
}
} else {
$filtered = $results;
}
$data = json_encode($results);
echo html_entity_decode( $data );
exit();
}
add_action( 'wp_ajax_my_search', 'my_search' );
add_action( 'wp_ajax_nopriv_my_search', 'my_search' );
我将 jQueryUI AutoComplete 与 WordPress 一起使用,以在用户在搜索框中键入内容时获取自定义分类的术语。
我有很多运动品牌作为自定义分类术语,因此,例如术语 Adidas
可以有很多子术语。现在当我输入 a
时,它显示...
Adidas
Adidas Shoes
Adidas Socks
- 等...
...导致其他品牌蒙在鼓里。所以,当我输入 a
时,我希望结果显示如下...
Adidas
Amphipod
Asics
- 等...
...直到我继续键入 ad
,然后才会出现带有子项的 Adidas
结果,就像第一个示例中那样。这有可能实现吗? jQueryUI AutoComplete 是否能够像这样为我排序结果,或者我是否需要在服务器端使用 PHP 函数(如 sort
、usort
等)对它们进行排序?到目前为止,我尝试编写了一些不同的 PHP 排序函数,但都无济于事,但我现在真的不知道。
这是我现在拥有的代码:
autocomplete.js
$(function() {
var url = MyAutocomplete.url + "?action=my_search";
$('.search-field').autocomplete({
source: url,
delay: 500,
minLength: 2,
sortResults: true
})
});
functions.php(WordPress)
function my_search()
{
$args = array(
'search' => strtolower($_GET['term']),
'taxonomy' => array('suggestion_string'),
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'number' => 10,
);
$search_query = new WP_Term_Query($args);
$results = array( );
if ( $search_query->get_terms() ) {
foreach($search_query->get_terms() as $term) {
$results[] = array(
'label' => $term->name,
);
}
}
else {
}
// Tried to write a few different sort functions here to no avail, like:
sort($results);
$data = json_encode($results);
echo html_entity_decode( $data );
exit();
}
add_action( 'wp_ajax_my_search', 'my_search' );
add_action( 'wp_ajax_nopriv_my_search', 'my_search' );
朋友,一切皆有可能,您只需按照自己的意愿编写代码即可。你可以在服务器端和客户端实现你想要的,在这种情况下我会推荐服务器端。
如果您不想自动完成显示子类别,请不要提供这些子类别。您要么首先消除以相同单词开头的名称,要么排除子类别。这是我为您提供的第一种可能性的代码:
function my_search()
{
$args = array(
'search' => strtolower($_GET['term']),
'taxonomy' => array('suggestion_string'),
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'number' => 10,
);
$search_query = new WP_Term_Query($args);
$results = array( );
if ( $search_query->get_terms() ) {
foreach($search_query->get_terms() as $term) {
$results[] = array(
'label' => $term->name,
);
}
}
else {
}
$filtered = [];
$existing = [];
if(strlen($_GET['term'])<2) {
foreach($results as $term) {
$first_word = explode(" ",$term["label"])[0];
if(!in_array($first_word,$existing) {
array_push($existing,$first_word);
$filtered[] = $term;
}
}
} else {
$filtered = $results;
}
$data = json_encode($results);
echo html_entity_decode( $data );
exit();
}
add_action( 'wp_ajax_my_search', 'my_search' );
add_action( 'wp_ajax_nopriv_my_search', 'my_search' );