There is a category on wordpress where posts and standard pagination 1, 2, 3, etc. are displayed. I made that when I clicked on the pagination, an Ajax request was triggered and from the next page of the pagination the posts were inserted into the page, but for some reason all the posts are displayed. Where am I wrong?
Post output code with pagination
<?php
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'cat' => $current_cat_id,
'posts_per_page' => 1,
'paged' => $paged,
);
$query = new WP_Query( $args );
// Цикл
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<a class="post-list-item mb-32" href="<?php the_permalink(); ?>">
<div class="post">
<div class="post-header mb-32">
<div class="post-date">Jan 4, 2022</div>
<h3 class="post-title"><?php the_title() ?></h3>
<div class="post-short-info">
<?php the_field('kratkoe_opisanie'); ?></div>
</div>
<div class="post-thumbnail">
<div class="image-inner"><img class="image"
src="<?php echo get_the_post_thumbnail_url(); ?>">
</div>
</div><span class="read-more">Read more</span>
</div>
</a>
<?php
}
} else {
// Постов не найдено
}
// Возвращаем оригинальные данные поста. Сбрасываем $post.
wp_reset_postdata();
$big = 999999999; // уникальное число
$pagination = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages
) );
?>
</div>
<?php
echo "<div class='pagination'>" . $pagination . "</div>";
?>
Ajax javascript
<script type="text/javascript">
$('.pagination a').click(function(e) {
e.preventDefault(); // don't trigger page reload
if($(this).hasClass('active')) {
return; // don't do anything if click on current page
}
$.post(
'<?php echo admin_url('admin-ajax.php'); ?>', // get admin-ajax.php url
{
action: 'ajax_pagination',
page: parseInt($(this).attr('data-page')), // get page number for "data-page" attribute
posts_per_page: <?php echo get_option('posts_per_page'); ?>
},
function(data) {
$('.posts-list').html(data); // replace posts with new one
console.log(data);
}
);
});
</script>
Functions.php
function my_ajax_navigation() {
$requested_page = intval($_POST['page']);
$posts_per_page = intval($_POST['posts_per_page']) - 1;
$posts = get_posts(array(
'cat' => 6,
'posts_per_page' => $posts_per_page,
'offset' => $page * $posts_per_page
));
foreach ($posts as $post) {
setup_postdata( $post );
echo $post->ID;
}
exit;
}
add_action( 'wp_ajax_ajax_pagination', 'my_ajax_navigation' );
add_action( 'wp_ajax_nopriv_ajax_paginationr', 'my_ajax_navigation' );