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
<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 } ?>
Just use a
break
http://php.net/manual/en/control-structures.break.php
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() {
Then:
Also there where a missing endforeach and I reset the postdata