How to stop infinite loop after the end of post list? WordPress

I build custom infinite loop for my blog. Everything is working great but if there is no posts to show then also my infinite loop is loading again and again with the other design parts. How to add conditions in my infinite loop so it can stop if post list ends.

AJAX to load infinite loop

Read More
<script>
        $(document).ready(function() {

        var post_page_count = 0;
        var height_scroll = 400;
            $(window).scroll(function() {
            if ($('body').height() <= ($(window).height() + $(window).scrollTop())){

                $.ajax({
                        type: "POST",
                        async: false,
                        url: "/loopa/infiloop.php",
                        data: {pcount:post_page_count},
                        success:
                        function(result){

                            $("#looppage").append(result);
                            }
                });
            post_page_count = post_page_count+20;

            }
});
});
</script>

The Loop I am using:

<?php

$infinite_loop= $_POST['pcount'];  ?>

<?php require('../wp-config.php');
$wp->init();
$wp->parse_request();
$wp->query_posts();
$wp->register_globals(); ?>

      <div class="myclass" role="main">
            <?php
                    global $wpdb;
                    $args = array( 'posts_per_page' => 20, 'order' => 'DESC', 'offset'=>$infinite_loop, 'category' => 613);    
                    $myposts = get_posts( $args );
                    foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
                    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>


                        <div>
                            <div class="gizinfi-img"> 
                                <?php giz_featured_index(); ?> 
                            </div>
                            <div class="gizinfi-title">
                                <?php /* print $args['offset']; */ ?>
                                <?php giz_get_view( 'gizc', '_content', 'post-header' ); ?>
                             </div>
                         </div>
                    </article>
</div>
<?php if (!giz_wp_is_mobile() ) { ?>
 <?php get_sidebar(); ?>
<?php } ?>

Related posts

Leave a Reply

2 comments

  1. Well I would say that this is not really an infinite loop, this is just the ajax being triggered without stop and this is triggered inclusive if there are no more posts left.
    So to fix this :
    `
    $(document).ready(function() {

        var post_page_count = 0;
        var height_scroll = 400;
        var no_more_posts = false;
        $(window).scroll(function() {
            if(no_more_posts) return;
            if ($('body').height() <= ($(window).height() + $(window).scrollTop())){
    
                $.ajax({
                    type: "POST",
                    async: false,
                    url: "/loopa/infiloop.php",
                    data: {pcount:post_page_count},
                    success:
                    function(result){
                        if(result=="done"){
                            no_more_posts = true;
                        }else{                          
                            $("#looppage").append(result);
                        }
                    }
                });
                post_page_count = post_page_count+20;
    
            }
        });
    });
    


    Then:

    $infinite_loop= $_POST['pcount'];  ?>
    
    <?php require('../wp-config.php');
    $wp->init();
    $wp->parse_request();
    $wp->query_posts();
    $wp->register_globals(); 
    $args = array( 'posts_per_page' => 20, 'order' => 'DESC', 'offset'=>$infinite_loop, 'category' => 613);    
    $myposts = get_posts( $args );
    if(!$myposts->found_posts){
        echo "done";
        die;
    }
    ?>
    
    
    <div class="myclass" role="main">
        <?php
        global $wpdb;
        foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <div>
                <div class="gizinfi-img"> 
                    <?php giz_featured_index(); ?> 
                </div>
                <div class="gizinfi-title">
                    <?php /* print $args['offset']; */ ?>
                    <?php giz_get_view( 'gizc', '_content', 'post-header' ); ?>
                </div>
            </div>
        </article>
        <?php endforeach ?>
        <?php wp_reset_postdata() ?>
    </div>
    <?php if (!giz_wp_is_mobile() ) { ?>
    <?php get_sidebar(); ?>
    <?php } ?>`
    

    Also there where a missing endforeach and I reset the postdata