簡體   English   中英

WordPress-如何按字母順序排列jQuery AutoComplete搜索建議結果,優先考慮第一個字符,然后第二個等等。

[英]WordPress - How to alphabetically order jQuery AutoComplete search suggestion results, prioritizing first character, then second etc.?

我正在使用WordPress的jQueryUI AutoComplete,以在用戶在搜索框中鍵入內容時獲取自定義分類的術語。

我有很多體育品牌作為自定義分類標准術語,因此,例如, Adidas這個詞可以有很多子術語。 現在,當我輸入a ,它將顯示...

  • Adidas
  • Adidas Shoes
  • Adidas Socks
  • 等等...

...導致其他品牌陷入困境。 因此,當我輸入a ,我希望結果顯示如下...

  • Adidas
  • Amphipod
  • Asics
  • 等等...

...直到我繼續輸入ad ,直到第一個示例中的Adidas結果和子項出現,才這樣。 這有可能實現嗎? jQueryUI AutoComplete是否可以像這樣為我排序結果,還是我需要使用諸如sortusort等PHP函數在服務器端對它們進行sort 到目前為止,我嘗試編寫一些不同的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' );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM