title: How to stop infinite loop after the end of post list? Wordpress
body: <p>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.</p>
  
  <p><strong>AJAX to load infinite loop</strong></p>
  
  <pre><code>&lt;script&gt;
          $(document).ready(function() {
  
          var post_page_count = 0;
          var height_scroll = 400;
              $(window).scroll(function() {
              if ($('body').height() &lt;= ($(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;
  
              }
  });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>The Loop I am using:</strong></p>
  
  <pre><code>&lt;?php
  
  $infinite_loop= $_POST['pcount'];  ?&gt;
  
  &lt;?php require('../wp-config.php');
  $wp-&gt;init();
  $wp-&gt;parse_request();
  $wp-&gt;query_posts();
  $wp-&gt;register_globals(); ?&gt;
  
        &lt;div class="myclass" role="main"&gt;
              &lt;?php
                      global $wpdb;
                      $args = array( 'posts_per_page' =&gt; 20, 'order' =&gt; 'DESC', 'offset'=&gt;$infinite_loop, 'category' =&gt; 613);    
                      $myposts = get_posts( $args );
                      foreach ( $myposts as $post ) : setup_postdata( $post ); ?&gt;
                      &lt;article id="post-&lt;?php the_ID(); ?&gt;" &lt;?php post_class(); ?&gt;&gt;
  
  
                          &lt;div&gt;
                              &lt;div class="gizinfi-img"&gt; 
                                  &lt;?php giz_featured_index(); ?&gt; 
                              &lt;/div&gt;
                              &lt;div class="gizinfi-title"&gt;
                                  &lt;?php /* print $args['offset']; */ ?&gt;
                                  &lt;?php giz_get_view( 'gizc', '_content', 'post-header' ); ?&gt;
                               &lt;/div&gt;
                           &lt;/div&gt;
                      &lt;/article&gt;
  &lt;/div&gt;
  &lt;?php if (!giz_wp_is_mobile() ) { ?&gt;
   &lt;?php get_sidebar(); ?&gt;
  &lt;?php } ?&gt;
  </code></pre>