帖子列表结束后如何停止无限循环?文字出版社


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

我为我的博客构建了自定义无限循环。一切正常,但是如果没有帖子可以显示,那么我的无限循环也会一次又一次地与其他设计部分一起加载。如何在我的无限循环中添加条件,以便在帖子列表结束时它可以停止。

AJAX 加载无限循环

<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>

我正在使用的循环:

<?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 } ?>

只需使用break

<?php $baz = array('foo', 'bar', 'baz');
foreach ($baz as $foo) {
    echo $foo;
    if ($foo == 'bar') {
        break();
    }
} ?>
// I haven't tested it, but it should output `foo``bar` then break.
// just replace the if check with a check for the last element.

http://php.net/manual/en/control-structures.break.php

好吧,我想说这不是一个真正的无限循环,这只是 ajax 在没有停止的情况下被触发,如果没有更多的帖子,这将被触发。所以要解决这个问题:` $(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 } ?>`

也在那里缺少一个endforeach,我重置了postdata