WordPress循环中的帖子重复

时间:2016-02-11 21:57:22

标签: php wordpress

我正在从头开始构建一个使用无限滚动的WordPress主题。问题是,帖子一遍又一遍地加载。

我已经粘贴了一个空白循环并且工作正常,只要我重新输入代码,循环就会重新开始。

我一点一点地查看了代码,但我看不出问题的原因 - 我想知道它是否与偏移有关?

循环标记在这里:

<?php
// get offset from $_POST object if its set else offset will be 1
$offset = isset( $_POST['offset'] ) ? intval( $_POST['offset'] ) : 0; 
$post_per_pages = isset( $_POST['offset'] ) ? 3 : 6 ;?>

<?php query_posts( 'posts_per_page=' . $post_per_pages . '&offset=' . $offset );?>

<?php if (have_posts()): while (have_posts()) : the_post(); ?>

<div class="work-item">

        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">

            <?php 
                $image = get_field('work-featured-image');
                $size = 'work-featured-image';
                if( $image ) {
                    echo wp_get_attachment_image( $image, $size );
                }
            ?>

            <div class="back">
                <div>
                    <h3><?php the_field('work-caption'); ?></h3>
                </div>
            </div>

        </a>

    </div>

<?php endwhile; ?>

<?php else: ?>

    <article>
        <h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
    </article>

<?php endif; ?>

2 个答案:

答案 0 :(得分:0)

您始终发送相同的偏移量 - 0。

改变这个 -

var offset =jQuery('.hentry').length;

进入

var offset = jQuery('.work-item').length;

答案 1 :(得分:0)

home.php吗?

尝试使用自定义查询替换query_posts( . . .)查询,如下所示:

<?php
// get offset from $_POST object if its set else offset will be 1
$offset = isset( $_POST['offset'] ) ? intval( $_POST['offset'] ) : 0; 
$post_per_pages = isset( $_POST['offset'] ) ? 3 : 6 ;?>

<?php 
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => $posts_per_page,
        'offset' => $offset
    );
    $posts = new WP_Query($args);

    if($posts->have_posts()):
    while($posts->have_posts()): $posts->the_post();
?>
<div class="work-item">

    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">

        <?php 
            $image = get_field('work-featured-image');
            $size = 'work-featured-image';
            if( $image ) {
                echo wp_get_attachment_image( $image, $size );
            }
        ?>

        <div class="back">
            <div>
                <h3><?php the_field('work-caption'); ?></h3>
            </div>
        </div>

    </a>

</div>

<?php 
    endwhile;
    wp_reset_postdata(); // this makes sure nested loops are not overlapped   
?>
<?php else: ?>

<article>
    <h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
</article>