Ajax WP_查询未显示分页的函数
我有这个代码和它的工作良好,唯一的问题是,它没有显示分页。这段代码在我的functions.php文件中Ajax WP_查询未显示分页的函数,ajax,wordpress,pagination,Ajax,Wordpress,Pagination,我有这个代码和它的工作良好,唯一的问题是,它没有显示分页。这段代码在我的functions.php文件中 add_action( 'wp_ajax_nopriv_load-filter', 'prefix_load_cat_posts' ); add_action( 'wp_ajax_load-filter', 'prefix_load_cat_posts' ); function prefix_load_cat_posts () { global $post; $cat_id
add_action( 'wp_ajax_nopriv_load-filter', 'prefix_load_cat_posts' );
add_action( 'wp_ajax_load-filter', 'prefix_load_cat_posts' );
function prefix_load_cat_posts () {
global $post;
$cat_id = $_POST[ 'cat' ];
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array (
'cat' => $cat_id,
'posts_per_page' => 6,
'order' => 'DESC',
'paged' => $paged
);
$cat_query = new WP_Query($args);
if($cat_query->have_posts()) :
while($cat_query->have_posts()) : $cat_query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
wp_reset_query();
?>
<div class="page-nation">
<ul class="pagination pagination-large">
<?php
$pagination = get_the_posts_pagination(array(
'mid_size' => 2,
'prev_text' =>esc_html__('Previous', 'travel-tour'),
'next_text' => esc_html__('Next', 'travel-tour'),
'screen_reader_text' => ' ',
) );
echo $pagination;
?>
</ul>
</div>
<?php
endif;
die();
}
这是我的jquery ajax脚本
<script>
jQuery(document).ready(function () {
jQuery('.js-category-button').on('click', function(e){
e.preventDefault();
jQuery('.preloadswitch').addClass('addpreloader');
var catID = jQuery(this).data('slug');
var catName = jQuery(this).attr('href');
var ajaxurl = '<?php echo esc_js( admin_url( 'admin-ajax.php' ) ) ?>';
jQuery.ajax({
type: 'POST',
url: ajaxurl,
crossDomain : true,
dataType: 'html',
data: {"action": "load-filter", cat: catID },
beforeSend: function () {
jQuery(".the-categories").html('<div></div>').fadeIn('slow');
jQuery(".page-nation").hide();
//window.location.hash = "#"+jQuery("#comehere").attr("id");
jQuery('html,body').animate({scrollTop:jQuery('#comehere').offset().top}, 1000);
window.history.pushState('obj', 'newtitle', catName);
},
success: function(response) {
jQuery(".the-categories").append(response);
jQuery('.preloadswitch').removeClass('addpreloader');
return false;
}
});
})
});
</script>
我不知道在这里做什么了,我一直在网上搜索可能的工作功能,但没有运气
我只需要显示分页注释掉或删除wp_reset_查询;,或在分页后将其移动到;使用而不是
以下是前缀\u load\u cat\u posts函数中经过修改的“if else”块:
if($cat_query->have_posts()) :
while($cat_query->have_posts()) : $cat_query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
//wp_reset_query();
?>
<div class="page-nation">
<ul class="pagination pagination-large">
<?php
$pagination = paginate_links(array(
'mid_size' => 2,
'prev_text' =>esc_html__('Previous', 'travel-tour'),
'next_text' => esc_html__('Next', 'travel-tour'),
'current' => $paged,
'total' => $cat_query->max_num_pages,
'type' => 'array',
'base' => home_url( '/%_%' ),
) );
echo '<li>' . implode( '</li><li>', $pagination ) . '</li>';
?>
</ul>
</div>
<?php
endif;
[编辑]尝试设置基本参数。Hi@Sally CJ,我已将其更改为paginate_links,并且其工作正常,但现在的问题是pagination link及其链接如下您好,它现在显示了pagination links,但没有重定向到其正确的页面,喜欢来自类别,因此每次单击链接时路径都会更改。好的,我可以帮你解决这个问题;但是请分享HTML标记。而且,你的问题已经得到了回答,对吗?我的意思是,这个问题:我只需要显示分页。不要管HTML标记。见我的评论。