Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have posts that load when I hit the bottom of a container, lazy load, infinite scroll etc. using the jquery waypoint plugin.

I have a button that dynamically loads the first 3 posts again, so I can scroll to load the posts again.

The problem is that it appears I run out of posts, as I'm running offset +3 each time I hit a waypoint. Ajaxing that number and retrieving a loop with offset => $offset.

It works fine if I refresh the page, so is there a funtion I can tie to the button that tells wordpress to make revert the offset back to zero without reloading the page?

Here's the JS I'm using and the loop I'm calling:

JS

jQuery(document).ready(function($){
    $("a.sort-all").bind("click", function(e) {
        load_posts(this);
        e.preventDefault();                 
    });

    function load_posts() {
        var ajax_url = $('.sort-all').attr('data-all-url');
        $.ajax({
            dataType: "HTML",
            url: ajax_url,
            type:'POST',
            data:{
                action: 'data_click_all'
            },
            success:function(data) {
                $('#content').html(data);
            }
        });

    }

    var ajax_url = $('.sort-all').attr('data-all-url');
    var offset = 0;
    $('#content').waypoint(function(direction) {
        if(direction === 'down'){
            offset = parseInt(offset) + 3;
            $.ajax({
                dataType: "HTML",
                url: ajax_url,
                type:'POST',
                data:{
                    action: 'data_scroll_all',
                    offset: offset
                },
                success:function(data) {
                    $('#content').append(data);
                    $.waypoints('refresh');
                }
            });
        }
    }, {
        offset: 'bottom-in-view'
    });
}); 

PHP

function data_scroll_all(){
            $offset = $_POST['offset'];
            $args = array('posts_per_page' => 3, 'category__not_in' => 1, 'offset' => $offset, 'orderby' => 'date');
            $loop = new WP_Query($args);
            if($loop->have_posts()) {
                while($loop->have_posts()){
                    $loop->the_post();
                    get_template_part( 'content-archive' );
                }
            } wp_reset_postdata();
            die('');
        }
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.