2010-12-05 92 views
0

我需要實現一個滑塊,它將顯示每張幻燈片中特定類別的4個帖子縮略圖。爲此,我寫了這個:檢索帶循環的wordpress帖子

<ul class= "videoSlider"> 
          <?php 
          $pStart = 0; 
          $flag = true; 

          while ($flag) { 

           query_posts('cat=14&posts_per_page=4&offset='.$pStart); 

           $pStart = + 4; 
          ?> 


           <li> 
           <?php 
           if (have_posts()) { 

            while (have_posts()) { 
             the_post(); 
           ?> 
             <div onclick="something()"> 

            <?php echo the_post_thumbnail(array(215, 190)); ?> 
             </div> 

           <?php 
            } 
           } else { 
            $flag = false; 
           } 
           ?> 
          </li> 


          <?php 

          wp_reset_query(); 

          } ?> 

我需要jQuery的滑塊的結構是這樣的:

   <ui> 
         <li> 
         <div> 
          thumb 1 
         </div> 
         <div> 
          thumb 2 
         </div> 
         <div> 
          thumb 3 
         </div> 
         <div> 
          thumb 4 
         </div> 
        </li> 


        <li> 
         <div> 
          thumb 5 
         </div> 
         <div> 
          thumb 6 
         </div> 
         <div> 
          thumb 7 
         </div> 
         <div> 
          thumb 8 
         </div> 
        </li> 

       </ul> 

但是代碼不工作因某種原因!看起來像生成幾個列表後,代碼執行不會停止,並且瀏覽器掛起。我是否以錯誤的方式使用了該功能:'query_posts('cat=14&posts_per_page=4&offset='.$pStart)'? 我應該如何實際執行它?

回答

0

如果你有很多帖子,那麼外層的while循環會一直持續,直到每次發帖查詢2次。

對我來說,就像你讓自己的事情變得複雜一樣,所以這就是我所要做的。

global $wp_query; 
query_posts('cat=14'); 

if (have_posts()): 

    $last_post = $wp_query->post_count - 1; // index for the last post 
    $counter = 0; 

    echo '<ul class= "videoSlider">'; 

     while (have_posts()): 

      the_post(); 

      if ($counter === 0) 
       echo '<li>'; 

      echo '<div onclick="something()">'; 
      the_post_thumbnail(array(215, 190)); 
      echo '</div>'; 

      if ($counter === 3 || $last_post == $wp_query->current_post) { 
       $counter = 0; 
       echo '</li>'; // close the tag every 4th item, or if we're at the end of the loop 

      } else { 
       $counter++; 
      } 


     endwhile; 

    echo '</ul>'; 

endif; 
+0

非常好!它工作得很好!我沒有這樣想:)注意:在我的第一個方法中,我嘗試了6個帖子,所以我不明白爲什麼它花了很長時間並凍結瀏覽器! – med 2010-12-06 06:27:31

相關問題