Here is js
(function($) { $('.pages_list a').click(function(e){ e.preventDefault(); postId = $(this).attr('data-catid'); $.ajax({ url: "/wp-admin/admin-ajax.php", method: 'post', data: { action: 'ajax_order', name: postId, countpost: '<? echo get_field("count_publications"); ?>' }, success: function (response) { $("#thumbs").html(response); } }); }); }(jQuery));
This code takes the id value of the posts category.
postId = $(this).attr('data-catid');
Here is the code in functions.php
add_action('wp_ajax_nopriv_ajax_order', 'ajax_form' ); add_action('wp_ajax_ajax_order', 'ajax_form' ); function ajax_form(){ $postId = $_REQUEST['name']; $countPublications = $_REQUEST['countpost']; if ( defined( 'DOING_AJAX' ) && DOING_AJAX ){ $paged = get_query_var( 'paged', $postId ); $recent = new WP_Query("cat=$postId&showposts=$countPublications&paged=$paged"); $postArr = array(); while($recent->have_posts()) : $recent->the_post(); $link = get_the_permalink(); $title = get_the_title(); if ( has_post_thumbnail() ) { $image = get_the_post_thumbnail('miniatures'); } else { $imagePath = bloginfo("template_url") . "/img/no_image.jpg"; $image = '<img src='.$imagePath.' alt="'.$title.'"/>'; } $thumbDesc = get_the_excerpt(); $postItem = '<a href='.$postId.' alt='.$title.' rel="bookmark" class="thumb_3"><div class="img-box">'.$image.'</div><h4>'.$title.'</h4>'.$thumbDesc.'</a>'; array_push($postArr, $postItem); endwhile; wp_reset_postdata(); echo $postArr; wp_die(); } }
This js
$("#thumbs").html(response);
Displays an empty array, not an array of HTML posts. How to display an array of posts when clicked?