2013-10-20 74 views
0

我有一個顯示HTML的while循環。更改while循環中的第一個元素?

但是,我只需要第一次迭代的不同元素。

我該如何管理?這是我的代碼:

<?php 
if (have_posts()) : 
    while (have_posts()) : 
     the_post(); 
     $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'single-post-thumbnail'); 
     ?> 

     <div class="item" style="background-size:cover; background-image: url(<?php echo $image[0]; ?>); width: 100%; min-height: 300px;"> 
      <div class="carousel-caption"> 
       <h3><?php the_title(); ?></h3> 
       <p><?php comments_number('Pas de commentaires', '1 commentaire', '% commentaires'); ?></p> 
      </div> 
     </div> 

     <?php 
    endwhile; 
endif; 
?> 

感謝您的幫助!

回答

3

試試這個代碼

<?phph $flag = 1; ?> 

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

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

    <?php if($flag) { 

        DO YOUR WORK; 


        $flag = 0; 
        } ?> 

    Rest of your code. 

當該腳本將首次將發現,該標誌爲真,所以你可以做一些與第一循環不同運行。那麼FLAG就會變成錯誤的。等下一個循環,它不會得到被用於第一僅環

1

你的完整代碼這個「如果」裏面:

<?php 
if (have_posts()) : 
    $flag = 1; 
    while (have_posts()) : the_post(); 
     if($flag == 1) { 

        PUT YOUR CODE HERE;  

        $flag = 0; 
        } 

     $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'single-post-thumbnail'); 
     ?> 

     <div class="item" style="background-size:cover; background-image: url(<?php echo $image[0]; ?>); width: 100%; min-height: 300px;"> 
      <div class="carousel-caption"> 
       <h3><?php the_title(); ?></h3> 
       <p><?php comments_number('Pas de commentaires', '1 commentaire', '% commentaires'); ?></p> 
      </div> 
     </div> 

     <?php 
    endwhile; 
endif; 
?> 

感謝。

相關問題