0

Good afternoon. Make loading posts via ajax, everything is fine, but! Displays when scrolling immediately on 200+ records and many duplicates, how to fix the code below, I would be very grateful for help

JS:

    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() > ( $(document).height() - 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 ); 
                        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', 
            'posts' => json_encode( $wp_query->query_vars ),
            '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; 
        $args['post_status'] = 'publish';
        $args ['showposts'] = 16;

        query_posts($args );

        if( have_posts() ) :

            while( have_posts() ): the_post(); 
             $thumbnail = wp_get_attachment_image_src ( get_post_thumbnail_id ( $post->ID ),full);
                $thumbnail1 = get_post_meta($post->ID, 'Thumbnail', true);
                $hit = get_post_meta($post->ID, 'Хит продаж', true);
                $old_price = get_post_meta($post->ID, 'old_price', true);
                $cost = get_post_meta($post->ID, 'cost_1', true);
                $minsale = get_post_meta($post->ID, 'Минимальное кол-во для заказа', true);
                $vnutkod = get_post_meta($post->ID, 'vnutrenniy_kod', true);
            ?>
           my html

    <?      endwhile;

        endif;
        die; 
    }


add_action('wp_ajax_loadmore', 'misha_loadmore_ajax_handler'); 
add_action('wp_ajax_nopriv_loadmore', 'misha_loadmore_ajax_handler'); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.