2016-02-11 163 views
-1

我從頭開始使用無限滾動的Wordpress主題。問題是,帖子不斷得到一遍又一遍的加載。WordPress的 - 循環中的重複帖子

我已經粘貼了一個空白循環,它工作正常,只要我把代碼放回來,循環再次開始。

我看過一點碼位,但我看不出問題的原因 - 我不知道是否有事情做與抵消...

誰能幫我找出這個問題?測試環節是here.

循環標記是在這裏:

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

回答

0

你總是發送相同的偏移 - 0。

改變這一點 -

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

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

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>