我需要設置wordpress ajax搜索結果,但是當按鈕被點擊後,我的方法沒有檢索結果,而是將我重定向到另一個網站(myurl.com? s = term)。我正確地調用了admin-ajax.php,但設置不正確。任何想法是什麼導致這個問題?如何使用jquery檢索wordpress的ajax搜索結果
//Script to activate ajax
jQuery(document).ready(function($){
var search_val=$("#s").val();
$('#searchsubmit').click(function(){
$.post(
WPaAjax.ajaxurl,
{
action : 'wpa56343_search',
search_val : search_val
},
function(response) {
$('#results').append(response);
}
);
});
});
//function to setup wp_query
add_action('wp_ajax_wpa56343_search', 'wpa56343_search');
function wpa56343_search(){
global $wp_query;
$search = $_POST['search_val'];
$args = array(
's' => $search,
'posts_per_page' => 5
);
$wp_query = new WP_Query($args);
get_template_part('search-results');
exit;
}
//html
<div id="my_search">
<form role="search" method="get" id="searchform" action="http://myurl.com/" >
<input type="text" value="" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" />
</form>
</div>
<div id="results"></div>
我的search.php是一個孩子的主題。這對.post行有什麼影響嗎? –
給你的search.php正確的網址,它在哪裏,檢查控制檯中的錯誤。 –
這是問題。謝謝你握住我的手! –