Pagination for aJax filters
-
I have a custom post type “news” that is formed as follows
add_action('init', function () { $labels = array( 'name' => 'News', 'singular_name' => 'News', 'add_new' => 'Add News', 'add_new_item' => 'Add News', 'edit_item' => 'Corrected news', 'new_item' => 'New news', 'all_items' => 'All news', 'search_items' => 'Search news', 'not_found' => 'News in these types were not found', 'not_found_in_trash' => 'No such news were found in the basket', 'menu_name' => 'News' ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => false, 'menu_icon' => 'dashicons-email-alt2', 'menu_position' => 5, 'supports' => array('title', 'editor', 'custom-fields', 'thumbnail'), 'taxonomies' => array('category', 'post_tag'), 'hierarchical' => true, 'has_archive' => true, 'query_var' => 'new', 'rewrite' => array( 'slug' => 'new', ), ); register_post_type('news', $args); });
It is necessary to make a filter some of chosen news.
The filter must work without reloading the page, and pagination must be present (no more than 5 records per page). functipn.php
add_action('wp_ajax_myfilter', 'my_filter_function'); add_action('wp_ajax_nopriv_myfilter', 'my_filter_function'); function my_filter_function(){ $my_terms = $_POST['filter_tags_id']; if(!empty( $my_terms)){ $args = array( 'post_type' => 'news', 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => '5', 'tax_query' => [ 'relation' => 'OR', [ 'taxonomy' => 'post_tag', 'field' => 'id', 'terms' => $my_terms, ] ], ); } else{ $args = array( 'post_type' => 'news', 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => -1, ); } $query = new WP_Query( $args ); if( $query->have_posts() ) : while ($query->have_posts()) : $query->the_post(); $title = get_the_title(); $publication_arr = get_post_custom_values('publication_img'); if (is_null($publication_arr) || $publication_arr[0] == '0') { $publication_img_src = get_template_directory_uri() . '/images/no-image.jpg'; } else { $publication_img_src = ''; } echo '<div class="block_item"> <img class="block_item__img" src="' . $publication_img_src . '" alt="' . $title . '"> <a class="block_item__link" href="' . get_permalink() . '">' . $title . '</a> </div>'; endwhile; ?> <div class="category_pagination"> <?php echo paginate_links( array( 'base' => get_pagenum_link(1) . '%_%', 'format' => 'page/%#%/', 'total' => $query->max_num_pages, 'current' => max( 1, get_query_var('paged') ), 'show_all' => false, 'end_size' => 1, 'mid_size' => 2, 'prev_next' => true, 'prev_text' => __('Previous', 'test'), 'next_text' => __('Next', 'test'), 'class' => '', ) ); ?> </div> <?php wp_reset_postdata(); else : echo '<div class="block_item">No news found</div>'; endif; die(); }
I made a page with filtering, but there is a problem with pagination. Pagination for filters does not work. The pagination links look like this.
<a class="page-numbers" >2</a>
How to fix this problem with pugination?
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Pagination for aJax filters’ is closed to new replies.