Jump to content
kuzovbmw

Wordpress при AJAX грузит все посты сразу + дублит их?

Recommended Posts

Пытаюсь по ajax подгружать посты, все бы ничего, но первые 16 выводятся нормально, после срабатывания события, выгружает штук 200 записей, хотя их всего 50, дублит их, а не подгружает по 8

скрипт ajax:
 

jQuery(function($){
  var canBeLoaded = true, 
      bottomOffset = 1500; 
 
  $(window).scroll(function(){
    var data = {
      'action': 'loadmore',
      'query': misha_loadmore_params.posts,
      'page' : misha_loadmore_params.current_page
    };
    if( $(document).scrollTop() > bottomOffset && canBeLoaded == true ){
      $.ajax({
        url : misha_loadmore_params.ajaxurl,
        data:data,
        type:'POST',
        beforeSend: function( xhr ){
        
          canBeLoaded = false; 
        },
        success:function(data){
          if( data ) {
            $('.category_hor_style').after( data ); // where to insert posts
            canBeLoaded = true; 
            misha_loadmore_params.current_page++;
          }
        }
      });
    }
  });
});



Обработчик php:
 

function misha_my_load_more_scripts() {
 
  global $wp_query; 
 
 
  wp_register_script('my_loadmore', get_stylesheet_directory_uri() . '/myloadmore.js', array('jquery') );
 

  wp_localize_script( 'my_loadmore', 'misha_loadmore_params', array(
    'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php', // WordPress AJAX
    'posts' => json_encode( $wp_query->query_vars ), // everything about your loop is here
    'current_page' => get_query_var( 'paged' ) ? get_query_var('paged') : 1,
    'max_page' => $wp_query->max_num_pages
  ) );
 
 	wp_enqueue_script( 'my_loadmore' );
}
 
add_action( 'wp_enqueue_scripts', 'misha_my_load_more_scripts' );


function misha_loadmore_ajax_handler(){
 
  $args = json_decode(stripslashes($_POST['query']), true);
  $args['paged'] = $_POST['page'] +1; // we need next page to be loaded
  $args['post_status'] = 'publish';
    $arg ['showposts'] = 16;
    
  query_posts( $args );
 
  if( have_posts() ) :
 
    
    while( have_posts() ): the_post(); 
  // тут моя верстка блоков 
 

 
    endwhile;
 
  endif;
  die; 
}
 
 
 
add_action('wp_ajax_loadmore', 'misha_loadmore_ajax_handler'); // wp_ajax_{action}
add_action('wp_ajax_nopriv_loadmore', 'misha_loadmore_ajax_handler'); // wp_ajax_nopriv_{action}

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...