如何在帖子列表结束后停止无限循环? WordPress的

时间:2015-03-10 08:41:47

标签: php wordpress foreach infinite-loop

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

加载无限循环的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 } ?>

2 个答案:

答案 0 :(得分:0)

只需使用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

答案 1 :(得分:0)

嗯,我会说这不是一个无限循环,这只是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