Answer the question
In order to leave comments, you need to log in
Ajax loading posts on click?
When you click on pagination, the post should change with Ajax, but in the array I display all the posts, and not page by page.
custom-template.php
<section class="section ps2">
<div class="s-wrap">
<div class="conteiner">
<div class="s-title">text</div>
<p>text</p>
<div class="double-tabs">
<ul class="change-tabs">
<?php
$categoryLists = get_field('category__list');
if($categoryLists):
foreach ($categoryLists as $categoryList):
?>
<li class=""><?php echo $categoryList->name?></li>
<?php endforeach;
endif;
?>
</ul>
<div class="tabs active">
<ul class="tabs__caption">
<? foreach ($categoryLists as $categoryList):
$subCategories = get_categories([
'parent' => $categoryList->term_id,
'hide_empty' => false
]);
foreach ($subCategories as $subCategory):?>
<li><?= $subCategory->name?></li>
<?php endforeach;?>
<?php endforeach;?>
</ul>
<?php
foreach ($categoryLists as $categoryList):
$subCategories = get_categories([
'parent' => $categoryList->term_id,
'hide_empty' => false
]);
foreach ($subCategories as $subCategory) :
global $news_id;
$news_id = $subCategory->term_id;
$catPosts = array(
'cat'=> $news_id,
'posts_per_page' => 1,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
);
$childPost = new WP_Query($catPosts);
?>
<div class="tabs__content active">
<div class="news">
<?php if($childPost->have_posts()):
while ( $childPost->have_posts() ) : $childPost->the_post();?>
<a href="" style="background-image: url('img')">
<p><?php the_title();?></p>
</a>
<?php endwhile;
endif;
wp_reset_postdata();
?>
<nav class="pagination" style="display: flex">
<?php
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $childPost->max_num_pages,
'prev_text' => '«',
'next_text' => '»'
) );
?>
</nav>
<?php wp_reset_postdata(); ?>
</div>
</div>
<?php endforeach;
endforeach;
?>
</div>
</div>
</div>
</div>
</section>
$(document).on( 'click', '.pagination a.page-numbers', function( event ) {
event.preventDefault();
var ajaxurl = "<?php echo admin_url('admin-ajax.php')?>";
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
'action': 'my_ajax_action',
'post_id': <?php echo $news_id; ?>
},
success : function(data) {
$('html').append(data);
console.log(data);
}
});
});
add_action('wp_ajax_my_ajax_action', 'my_ajax_action' );
add_action('wp_ajax_nopriv_my_ajax_action', 'my_ajax_action' );
function my_ajax_action() {
$args = $_POST['post_id'];
$my_posts = array(
'cat'=> $args,
'posts_per_page' => 1,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
);
$loop = new WP_Query( $my_posts );?>
<?php if($loop->have_posts()):
$data = [];
while ( $loop->have_posts() ) : $loop->the_post();
$data = the_title();
?>
<?php endwhile;
wp_send_json_success( $data );
endif;
die;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question