如何修复搜索自动完成wordpress中的字符

时间:2015-05-11 12:20:25

标签: php wordpress autocomplete character

我在我的wordpress主题中添加了一个自动完成功能,并且它正在显示帖子的标题,但是我对字符有一些问题

自动填充中的搜索结果是这样的,

Selena Gomez – Come & Get It

而不是这一个 Selena Gomez - Come&得到它

我的js代码在这里:

(function( $ ) {
    $(function() {
        var url = MyAutocomplete.url + "?action=my_search";
        $( "#s" ).autocomplete({
            source: url,
            delay: 500,
            minLength: 3
        }); 
    });

})( jQuery );

我的php函数就是这个

function add_scripts() {
    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'jquery-ui-autocomplete' );
    wp_register_style( 'jquery-ui-styles','http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css' );
    wp_enqueue_style( 'jquery-ui-styles' );
    wp_register_script( 'my-autocomplete', get_template_directory_uri() . '/js/autocomplete.js', array( 'jquery', 'jquery-ui-autocomplete' ), '1.0', false );
    wp_localize_script( 'my-autocomplete', 'MyAutocomplete', array( 'url' => admin_url( 'admin-ajax.php' ) ) );
    wp_enqueue_script( 'my-autocomplete' );
}

add_action( 'wp_enqueue_scripts', 'add_scripts' );
function my_search() {
        $term = strtolower( $_GET['term'] );
        $suggestions = array();

        $loop = new WP_Query( 's=' . $term );

        while( $loop->have_posts() ) {
            $loop->the_post();
            $suggestion = array();
            $suggestion['label'] = get_the_title();
            $suggestion['link'] = get_permalink();

            $suggestions[] = $suggestion;
        }

        wp_reset_query();


        $response = json_encode( $suggestions );
        echo $response;
        exit();

}
add_action( 'wp_ajax_my_search', 'my_search' );
add_action( 'wp_ajax_nopriv_my_search', 'my_search' );

我错了,任何人都可以知道这个问题或如何解决它?

0 个答案:

没有答案