在我的主页上,我显示了一个帖子循环,我允许用ajax搜索结果和其他自定义循环来替换。这些结果将在带有custom wp_query
. 问题是next_posts_link
没有为搜索循环返回第2页的正确链接。我得到的是链接http://mysite.com/wp-admin/admin-ajax.php?paged=2
. 下面是我用来返回结果的整个设置。我需要更改什么才能获得正确的分页?
//脚本
jQuery(document).ready(function($){
$(\'#searchsubmit\').click(function(e){
$(\'#boxes\').empty();
e.preventDefault();
var search_val=$("#s").val();
var $panel = $(this).closest(".panel-container").find(".panel");
$.post(
WPaAjax.ajaxurl,
{
action : \'wpa56343_search\',
search_val : search_val
},
function( response ) {
$(\'#boxes\').append( response ).masonry( \'reload\' );
$panel.hide("slow");
$(\'.trigger\').removeClass("active");
}
);
});
});
//传递查询参数的函数
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\' => 10
);
$wp_query = new WP_Query( $args );
get_template_part( \'search-results\' );
exit;
}
//自定义搜索查询搜索结果。php
<?php
while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<------content----->
<?php endwhile; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div class="infinitescroll">
<?php next_posts_link( __( \'Load more posts\', $wp_query->max_num_pages ) ); ?>
</div>
<script type="text/javascript">
// Infinite Scroll
var href = \'first\';
$(document).ready(function() {
$(\'#boxes\').infinitescroll({
navSelector : \'.infinitescroll\',
nextSelector : \'.infinitescroll a\',
itemSelector : \'#boxes .box\',
loadingImg : \'<?php echo get_bloginfo(\'stylesheet_directory\') ?>/images/loading.gif\',
loadingText : \'Loading...\',
donetext : \'No more pages to load.\',
debug : false
}, function(arrayOfNewElems) {
$(\'#boxes\').masonry(\'appended\', $(arrayOfNewElems));
if (href != $(\'.infinitescroll a\').attr(\'href\'))
{
href = $(\'.infinitescroll a\').attr(\'href\');
}
});
});
</script>
<?php endif; ?>