1
我想創建一個使用Ajax和jQuery的自定義WordPress主題的實時搜索。如果我使用.click()函數,它會顯示所需的輸出,但在使用.keyup或on('input',function(){})時,它不顯示任何內容。WordPress的Live搜索不顯示任何結果
jQuery代碼function.php的
var searchRequest = null;
var minlength = 3;
$(".search-submit").on('input',function(e){
e.preventDefault();
var st=$(".search-field").val();
if (st.length >= minlength)
{
if (searchRequest != null)
searchRequest.abort();
searchRequest=$.ajax({
type:"POST",
url: "./wp-admin/admin-ajax.php",
data: {
action:'wpa56343_search',
search_string:st
},
success:function(data){
$('.srch-result').append(data);
}
});
}
});
PHP代碼
add_action('wp_ajax_nopriv_wpa56343_search', 'wpa56343_search');
add_action('wp_ajax_wpa56343_search', 'wpa56343_search');
function wpa56343_search()
{
global $wp_query;
$search = $_POST['search_string'];
$args = array(
's' => $search,
'posts_per_page' => 5
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) :
$query->the_post();
get_template_part('template-parts/ajax-search-format', get_post_format());
endwhile;
endif;
wp_reset_postdata();
}
得到它。這是一個愚蠢的錯誤。感謝幫助 –