2012-03-05 42 views
0
最後一頁

我嘗試使用兩個列,以確保彼此配合,以顯示我的帖子,我試圖創建兩個垂直列,並顯示後奇數和偶數seperately。奇數在左側,甚至在右側。並且爲了讓wp_pagenavi()獲得導航工作,其他頁面正常工作,按照我的意圖顯示帖子,但在最後一頁上,一個帖子會重複兩次。WordPress的崗位重複兩次

這裏我對循環部分

<?php if (have_posts()) : ?> 

      <?php /* Start the Loop */ ?> 
     <div id="holder" class="left"> 

      <?php while (have_posts()) : ?> 
      <?php 
       $postcount++; 
       if(($postcount % 2) == 0) : // skip 'even' posts 
       $wp_query->next_post(); 
       else : 
       ?> 

       <?php the_post(); ?> 

      <div class="fpost"> 
       <div class="fposttitle"> 
        <h1 class="left"><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__('Permalink to %s', 'toolbox'), the_title_attribute('echo=0')); ?>" rel="bookmark"><?php the_title(); ?></a></h1> 
        <?php if (comments_open() || ('0' != get_comments_number() && ! comments_open())) : ?> 
         <div class="combox right"> 
          <?php $x = get_comments_number(); if ($x < 10) : ?> 
           <?php comments_popup_link(__('00', 'toolbox'), __('01', 'toolbox'), __('0%', 'toolbox')); ?> 
          <?php else : ?> 
           <?php comments_popup_link(__('00', 'toolbox'), __('01', 'toolbox'), __('%', 'toolbox')); ?> 
          <?php endif; ?> 
         </div> 
        <?php endif; ?> 
        <div class="clear"></div> 
       </div> 

       <div class="fcontent"> 
        <?php $thumbnail = get_post_meta(get_the_ID(), 'thumbnail', true); 
        if ($thumbnail) : ?> 
         <img src="<?php echo $thumbnail; ?>" alt="<?php the_permalink(); ?>" /> 
        <?php else : ?> 
         <p><?php the_excerpt(); ?></p> 
        <?php endif; ?> 
       </div> 

       <div class="finfo"> 
        <p class="left date"><?php the_time('F j, Y') ?></p> 
        <p class="left cat"><?php the_category(', ') ?></p> 
        <p class="right"><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__('Permalink to %s', 'toolbox'), the_title_attribute('echo=0')); ?>" rel="bookmark" >Read More</a></p> 
        <div class="clear"></div> 
       </div> 
      </div> 

      <?php endif; ?> 
      <?php endwhile; ?> 

     </div><!-- #content --> 

     <div id="holder2" class="left"> 

      <?php while (have_posts()) : ?> 
      <?php 
       $postcount++; 
       if(($postcount % 2) != 0) : // skip 'odd' posts 
       $wp_query->next_post(); 

       else : 
       ?> 

       <?php the_post(); ?> 

      <div class="fpost"> 
       <div class="fposttitle"> 
        <h1 class="left"><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__('Permalink to %s', 'toolbox'), the_title_attribute('echo=0')); ?>" rel="bookmark"><?php the_title(); ?></a></h1> 
        <?php if (comments_open() || ('0' != get_comments_number() && ! comments_open())) : ?> 
         <div class="combox right"> 
          <?php $x = get_comments_number(); if ($x < 10) : ?> 
           <?php comments_popup_link(__('00', 'toolbox'), __('01', 'toolbox'), __('0%', 'toolbox')); ?> 
          <?php else : ?> 
           <?php comments_popup_link(__('00', 'toolbox'), __('01', 'toolbox'), __('%', 'toolbox')); ?> 
          <?php endif; ?> 
         </div> 
        <?php endif; ?> 
        <div class="clear"></div> 
       </div> 

       <div class="fcontent"> 
        <?php $thumbnail = get_post_meta(get_the_ID(), 'thumbnail', true); 
        if ($thumbnail) : ?> 
         <img src="<?php echo $thumbnail; ?>" alt="<?php the_permalink(); ?>" /> 
        <?php else : ?> 
         <p><?php the_excerpt(); ?></p> 
        <?php endif; ?> 
       </div> 

       <div class="finfo"> 
        <p class="left date"><?php the_time('F j, Y') ?></p> 
        <p class="left cat"><?php the_category(', ') ?></p> 
        <p class="right"><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__('Permalink to %s', 'toolbox'), the_title_attribute('echo=0')); ?>" rel="bookmark" >Read More</a></p> 
        <div class="clear"></div> 
       </div> 
      </div> 

      <?php endif; ?> 
      <?php endwhile; ?> 

     </div><!-- #content --> 



     <?php else : ?> 

      no post 


    <?php endif; ?> 

     <div class="clear"></div> 
      <div id="nav"> <?php wp_pagenavi() ?> </div> 
    </div><!-- #primary --> 

我想代碼也許是因爲最後一頁,沒有足夠數量的職位,但無法弄清楚,爲什麼後重復。

更新:我只是想添加一個新的崗位,一切工作正常。但是當我添加另一篇文章時,它的最後一頁再次重複相同的文章。

+0

我認爲你應該每個循環之前你postCount重置爲0。在'<?php while(have_posts()):?>'你應該包含'$ postCount = 0;' – 2012-03-05 17:20:37

+0

@BrianGarson nope,嘗試它,不起作用仍然是相同的結果。它的工作原理是 – 2012-03-05 17:25:09

回答

1

大概應該包括這作爲一個答案,而不是評論,在你有奇數個崗位的情況下,不要將您的$ postcount變量重置爲0,那麼你的第二循環並不一定啓動在第一個循環所做的相同條件下。所以,你的最後一頁上你能看到這種情況發生,但機會是在你完全跳過某些職位的其他頁面:

 <?php if (have_posts()) : ?> 

     <?php /* Start the Loop */ ?> 
     <div id="holder" class="left"> 
     <?php $postcount = 0; ?> 
     <?php while (have_posts()) : ?> 
     <?php 
      $postcount++; 
      if(($postcount % 2) == 0) : // skip 'even' posts 
      $wp_query->next_post(); 
      else : 
      ?> 
      <?php the_post(); ?> 
     <?php endif; ?> 
     <?php endwhile; ?> 
     ... 
    </div><!-- #content --> 

    <div id="holder2" class="left"> 
    <?php $postcount = 0; ?> 
     <?php rewind_posts();//rewind the post counter so you can do another loop ?> 
     <?php while (have_posts()) : ?> 
     <?php 
      $postcount++; 
      if(($postcount % 2) != 0) : // skip 'odd' posts 
      $wp_query->next_post(); 

      else : 
      ?> 

      <?php the_post(); ?> 

     ... 

     <?php endif; ?> 
     <?php endwhile; ?> 

    </div><!-- #content --> 
+0

。非常感謝你,也許我在評論中提到它的時候把它放在了錯誤的地方。謝謝 – 2012-03-05 17:31:14

+0

我認爲它在每個循環之前都設置爲0。 NP :) – 2012-03-05 17:38:03