Advertisement
Barbareshet

ajax pagination

Dec 4th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.60 KB | None | 0 0
  1. <?php
  2. //in functions.php
  3. function bar_2_share_scripts() {
  4. wp_enqueue_script('top-five-ajax-pagination', get_stylesheet_directory_uri() . '/assets/js/pagination.js', array('jquery'), microtime().'', true);
  5.  global $wp_query;
  6.     wp_localize_script( 'top-five-ajax-pagination', 'ajaxpagination', array(
  7.         'ajaxurl' => admin_url( 'admin-ajax.php' ),
  8.         'query_vars' => json_encode( $wp_query->query )
  9.     ));
  10. }
  11. add_action( 'wp_enqueue_scripts', 'bar_2_share_scripts' );
  12.  
  13. function bar_ajax_pagination() {
  14.     $p_type = array('articles', 'post');
  15.     $_POST['postType'] = $p_type;
  16.     $paged = $_POST['page'];
  17.  
  18.     $ppp = 6;
  19.     $do_not_duplicate[] = get_the_ID();
  20.     $query_vars = [
  21.         'paged'             => $paged,
  22.         'post_type'         => $p_type,
  23.         'orderby'           =>'date',
  24.         'order'             => 'DESC',
  25.         'posts_per_page'    => $ppp,
  26.         'post_parent'       => 0,
  27.         'post__not_in'      => $do_not_duplicate
  28.  
  29.  
  30.     ];
  31.  
  32.     // $query_vars['paged'] = $_POST['page'];
  33.  
  34.  
  35.     $posts = new WP_Query( $query_vars );
  36.  
  37.     $GLOBALS['wp_query'] = $posts;
  38.  
  39.     if( ! $posts->have_posts() ) {
  40.         get_template_part( 'content', 'none' );
  41.     }
  42.     else {
  43.         while ( $posts->have_posts() ) {
  44.  
  45.             $posts->the_post();
  46.  
  47.             get_template_part( 'template-parts/content', $_POST['postType'] );
  48.         }
  49. //        $posts->rewind_posts();
  50.         wp_reset_query();
  51.     }
  52.  
  53.     die();
  54. }
  55.  
  56. add_action( 'wp_ajax_nopriv_ajax_pagination', 'bar_ajax_pagination' );
  57. add_action( 'wp_ajax_ajax_pagination', 'bar_ajax_pagination' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement